1 /* SPDX-License-Identifier: GPL-2.0-only */
14 /* Place the following struct somewhere in the ROM: */
15 struct SMC_BIOS_Update
{
16 uint8_t magic0
[4]; // always 0xaa00b1ed
17 char magic1
[4]; // always $FID
18 uint8_t magic2
[2]; // always 0x7804
19 uint8_t space0
; // always zero
20 // SMCinfotool doesn't care for the first letter
21 // The BMC webinterface does.
22 char boardid
[9]; // "100000000"
23 uint8_t space1
[15]; // unknown data
24 uint8_t space2
; // always 0x1f
25 char ukn_majorVer
[2];// unknown
26 uint8_t space3
; // always zero
27 char ukn_minorVer
[2];// unknown
28 uint8_t space4
; // always zero
29 char majorVer
[3]; // BIOS major version
30 char minorVer
[2]; // BIOS minor version
31 uint8_t space5
; // always zero
32 uint16_t year
; // year
33 uint8_t month
; // month
35 uint32_t space6
; // unknown data
36 uint8_t space7
; // all ones
37 char str
[15]; // "SUPERMSMCI--MB1"
38 uint8_t space8
[3]; // always zero
39 uint64_t space9
[6]; // all ones
42 static const char *optstring
= "b:i:o:h";
44 static struct option long_options
[] = {
45 {"boardid", required_argument
, 0, 'b' },
46 {"input", required_argument
, 0, 'i' },
47 {"output", required_argument
, 0, 'o' },
48 {"help", no_argument
, 0, 'h' },
51 static void usage(void)
53 printf("smcbiosinfo: Create BIOSInfo for BMC BIOS updates\n");
54 printf("Usage: smcbiosinfo [options] -i build.h -b <boardid> -o <filename>\n");
55 printf("-b | --boardid <ID> The board ID assigned by SMC\n");
56 printf("-i | --input <FILE> The build.h file to parse\n");
57 printf("-o | --output <FILE> The file to generate\n");
58 printf("-h | --help Print this help\n");
61 static int bcd2int(int hex
)
65 return ((hex
& 0xF0) >> 4) * 10 + (hex
& 0x0F);
68 static char *get_line(char *fn
, char *match
)
75 FILE *fp
= fopen(fn
, "r");
77 fprintf(stderr
, "E: Couldn't open file '%s'\n", fn
);
81 while ((read
= getline(&line
, &len
, fp
)) != -1) {
82 if (strstr(line
, match
) != NULL
) {
83 ret
= strdup(strstr(line
, match
) + strlen(match
));
89 fprintf(stderr
, "E: %s not found in %s\n", match
, fn
);
95 static int get_line_as_int(char *fn
, char *match
, int bcd
)
98 char *s
= get_line(fn
, match
);
99 if (s
&& strlen(s
) > 0) {
101 ret
= strtol(s
, &endptr
, 0);
102 if (*endptr
!= '\0' && *endptr
!= '\n') {
103 fprintf(stderr
, "E: Couldn't parse number for key '%s'\n", match
);
110 fprintf(stderr
, "E: Got invalid line for key '%s'\n", match
);
116 int main(int argc
, char **argv
)
120 char *filename
= NULL
;
121 char *inputfilename
= NULL
;
122 char *boardid
= NULL
;
128 c
= getopt_long(argc
, argv
, optstring
, long_options
, &optindex
);
135 boardid
= strdup(optarg
);
138 inputfilename
= strdup(optarg
);
141 filename
= strdup(optarg
);
144 ret
= 0; /* fallthrough */
153 if (!inputfilename
) {
154 fprintf(stderr
, "E: Must specify build.h filename\n");
158 fprintf(stderr
, "E: Must specify a destination filename\n");
162 if (!boardid
|| strlen(boardid
) == 0) {
163 fprintf(stderr
, "E: Board ID must be set\n");
166 if (strlen(boardid
) > 8) {
167 fprintf(stderr
, "E: Board ID must be less than 8 characters\n");
171 // generate the table
173 struct SMC_BIOS_Update sbu
= {
174 {0xed, 0xb1, 0x00, 0xaa},
178 "100000000", // boardid
181 "05", // unknown data
183 "06", // unknown data
194 {0, 0, 0}, // all zero
195 {~0, ~0, ~0, ~0, ~0, ~0}, // all ones
198 num
= get_line_as_int(inputfilename
, "COREBOOT_MAJOR_VERSION", 0);
204 snprintf(tmp
, sizeof(tmp
), "%03d", num
);
205 memcpy(&sbu
.majorVer
, &tmp
, sizeof(sbu
.majorVer
));
207 fprintf(stderr
, "E: Unsupported coreboot major version\n");
211 num
= get_line_as_int(inputfilename
, "COREBOOT_MINOR_VERSION", 0);
217 snprintf(tmp
, sizeof(tmp
), "%02d", num
);
218 memcpy(&sbu
.minorVer
, &tmp
, sizeof(sbu
.minorVer
));
220 fprintf(stderr
, "E: Unsupported coreboot minor version\n");
224 num
= get_line_as_int(inputfilename
, "COREBOOT_BUILD_YEAR_BCD", 1);
227 sbu
.year
= 2000 + num
;
229 num
= get_line_as_int(inputfilename
, "COREBOOT_BUILD_MONTH_BCD", 1);
234 num
= get_line_as_int(inputfilename
, "COREBOOT_BUILD_DAY_BCD", 1);
239 memcpy(&sbu
.boardid
[1], boardid
, strlen(boardid
));
242 FILE *fd
= fopen(filename
, "wb");
244 fprintf(stderr
, "E: %s open failed: %s\n", filename
, strerror(errno
));
248 if (fwrite(&sbu
, 1, sizeof(sbu
), fd
) != sizeof(sbu
)) {
249 fprintf(stderr
, "E: %s write failed: %s\n", filename
, strerror(errno
));
255 fprintf(stderr
, "E: %s close failed: %s\n", filename
, strerror(errno
));
262 fprintf(stderr
, "E: Error creating '%s'\n", filename
);