Skip to content

Commit 929aa8e

Browse files
committed
[ot] hw/opentitan: ot_rom_ctrl_img: improve VMEM detection resilience
srecord can't help adding a useless comment before the generated VMEM file Skip what may look like a C comment to better detect text file format. Signed-off-by: Emmanuel Blot <[email protected]>
1 parent 17d25f2 commit 929aa8e

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

hw/opentitan/ot_rom_ctrl_img.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static OtRomImgFormat ot_rom_img_guess_image_format(const char *filename)
4848

4949
uint8_t data[128u];
5050
ssize_t len = read(fd, data, sizeof(data));
51+
data[sizeof(data) - 1u] = '\0';
5152
close(fd);
5253

5354
if (len < sizeof(data)) {
@@ -58,10 +59,28 @@ static OtRomImgFormat ot_rom_img_guess_image_format(const char *filename)
5859
return OT_ROM_IMG_FORMAT_ELF;
5960
}
6061

61-
if (data[0] == '@') { /* likely a VMEM file */
62+
/*
63+
* Discard comments; only cope with single-line comments:
64+
* we do not need to handle more, since OT-generated files do not contain
65+
* multi-line comments in VMEM files; keep it as simple as possible.
66+
*/
67+
if (data[0u] == '/' && (data[1u] == '*' || data[1u] == '/')) {
68+
for (unsigned ix = 2u; ix < sizeof(data) - 1u; ix++) {
69+
if (data[ix] == '\n') {
70+
unsigned rem = sizeof(data) - 1u - ix;
71+
memmove(&data[0], &data[ix + 1u], rem);
72+
len = read(fd, &data[rem], sizeof(data) - rem);
73+
(void)len; /* GCC unused result warning */
74+
data[sizeof(data) - 1u] = '\0';
75+
break;
76+
}
77+
}
78+
}
79+
80+
if (data[0u] == '@') { /* likely a VMEM file */
6281
bool addr = true;
63-
unsigned dlen = 0;
64-
for (unsigned ix = 1; ix < sizeof(data); ix++) {
82+
unsigned dlen = 0u;
83+
for (unsigned ix = 1u; ix < sizeof(data); ix++) {
6584
if (data[ix] == ' ') { /* separator */
6685
if (addr) {
6786
addr = false;
@@ -87,9 +106,9 @@ static OtRomImgFormat ot_rom_img_guess_image_format(const char *filename)
87106
}
88107

89108
bool hexa_only = true;
90-
unsigned cr = 0;
109+
unsigned cr = 0u;
91110
unsigned ix;
92-
for (ix = 0; ix < sizeof(data); ix++) {
111+
for (ix = 0u; ix < sizeof(data); ix++) {
93112
if (data[ix] == '\r') {
94113
cr = ix;
95114
continue;

0 commit comments

Comments
 (0)