| 1 |
|
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <string.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <unistd.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
#include <getopt.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <stdarg.h> |
| 13 |
|
| 14 |
|
| 15 |
struct SMC_BIOS_Update { |
| 16 |
uint8_t magic0[4]; |
| 17 |
char magic1[4]; |
| 18 |
uint8_t magic2[2]; |
| 19 |
uint8_t space0; |
| 20 |
|
| 21 |
|
| 22 |
char boardid[9]; |
| 23 |
uint8_t space1[15]; |
| 24 |
uint8_t space2; |
| 25 |
char ukn_majorVer[2]; |
| 26 |
uint8_t space3; |
| 27 |
char ukn_minorVer[2]; |
| 28 |
uint8_t space4; |
| 29 |
char majorVer[3]; |
| 30 |
char minorVer[2]; |
| 31 |
uint8_t space5; |
| 32 |
uint16_t year; |
| 33 |
uint8_t month; |
| 34 |
uint8_t day; |
| 35 |
uint32_t space6; |
| 36 |
uint8_t space7; |
| 37 |
char str[15]; |
| 38 |
uint8_t space8[3]; |
| 39 |
uint64_t space9[6]; |
| 40 |
} __packed; |
| 41 |
|
| 42 |
static const char *optstring = "b:i:o:h"; |
| 43 |
|
| 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' }, |
| 49 |
}; |
| 50 |
|
| 51 |
static void usage(void) |
| 52 |
{ |
| 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"); |
| 59 |
} |
| 60 |
|
| 61 |
static int bcd2int(int hex) |
| 62 |
{ |
| 63 |
if (hex > 0xff) |
| 64 |
return -1; |
| 65 |
return ((hex & 0xF0) >> 4) * 10 + (hex & 0x0F); |
| 66 |
} |
| 67 |
|
| 68 |
static char *get_line(char *fn, char *match) |
| 69 |
{ |
| 70 |
ssize_t read; |
| 71 |
char *line = NULL; |
| 72 |
char *ret = NULL; |
| 73 |
size_t len = 0; |
| 74 |
|
| 75 |
FILE *fp = fopen(fn, "r"); |
| 76 |
if (fp == NULL) { |
| 77 |
fprintf(stderr, "E: Couldn't open file '%s'\n", fn); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
while ((read = getline(&line, &len, fp)) != -1) { |
| 82 |
if (strstr(line, match) != NULL) { |
| 83 |
ret = strdup(strstr(line, match) + strlen(match)); |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (!ret) |
| 89 |
fprintf(stderr, "E: %s not found in %s\n", match, fn); |
| 90 |
|
| 91 |
fclose(fp); |
| 92 |
return ret; |
| 93 |
} |
| 94 |
|
| 95 |
static int get_line_as_int(char *fn, char *match, int bcd) |
| 96 |
{ |
| 97 |
int ret = -1; |
| 98 |
char *s = get_line(fn, match); |
| 99 |
if (s && strlen(s) > 0) { |
| 100 |
char *endptr; |
| 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); |
| 104 |
free(s); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
if (bcd) |
| 108 |
ret = bcd2int(ret); |
| 109 |
} else { |
| 110 |
fprintf(stderr, "E: Got invalid line for key '%s'\n", match); |
| 111 |
} |
| 112 |
|
| 113 |
free(s); |
| 114 |
return ret; |
| 115 |
} |
| 116 |
|
| 117 |
int main(int argc, char **argv) |
| 118 |
{ |
| 119 |
int c; |
| 120 |
int ret = 1; |
| 121 |
char *filename = NULL; |
| 122 |
char *inputfilename = NULL; |
| 123 |
char *boardid = NULL; |
| 124 |
int num; |
| 125 |
|
| 126 |
while (1) { |
| 127 |
int optindex = 0; |
| 128 |
|
| 129 |
c = getopt_long(argc, argv, optstring, long_options, &optindex); |
| 130 |
|
| 131 |
if (c == -1) |
| 132 |
break; |
| 133 |
|
| 134 |
switch (c) { |
| 135 |
case 'b': |
| 136 |
boardid = strdup(optarg); |
| 137 |
break; |
| 138 |
case 'i': |
| 139 |
inputfilename = strdup(optarg); |
| 140 |
break; |
| 141 |
case 'o': |
| 142 |
filename = strdup(optarg); |
| 143 |
break; |
| 144 |
case 'h': |
| 145 |
ret = 0; |
| 146 |
case '?': |
| 147 |
usage(); |
| 148 |
goto out; |
| 149 |
default: |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if (!inputfilename) { |
| 155 |
fprintf(stderr, "E: Must specify build.h filename\n"); |
| 156 |
goto out; |
| 157 |
} |
| 158 |
if (!filename) { |
| 159 |
fprintf(stderr, "E: Must specify a destination filename\n"); |
| 160 |
goto out; |
| 161 |
} |
| 162 |
|
| 163 |
if (!boardid || strlen(boardid) == 0) { |
| 164 |
fprintf(stderr, "E: Board ID must be set\n"); |
| 165 |
goto out; |
| 166 |
} |
| 167 |
if (strlen(boardid) > 8) { |
| 168 |
fprintf(stderr, "E: Board ID must be less than 8 characters\n"); |
| 169 |
goto out; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
struct SMC_BIOS_Update sbu = { |
| 175 |
{0xed, 0xb1, 0x00, 0xaa}, |
| 176 |
"$FID", |
| 177 |
{0x04, 0x78}, |
| 178 |
0, |
| 179 |
"100000000", |
| 180 |
{}, |
| 181 |
0x1f, |
| 182 |
"05", |
| 183 |
0, |
| 184 |
"06", |
| 185 |
0, |
| 186 |
"000", |
| 187 |
"00", |
| 188 |
0, |
| 189 |
0, |
| 190 |
0, |
| 191 |
0, |
| 192 |
0, |
| 193 |
0xff, |
| 194 |
"SUPERMSMCI--MB1", |
| 195 |
{0, 0, 0}, |
| 196 |
{~0, ~0, ~0, ~0, ~0, ~0}, |
| 197 |
}; |
| 198 |
|
| 199 |
num = get_line_as_int(inputfilename, "COREBOOT_MAJOR_VERSION", 0); |
| 200 |
if (num < 0) |
| 201 |
goto out; |
| 202 |
|
| 203 |
if (num < 999) { |
| 204 |
char tmp[4]; |
| 205 |
snprintf(tmp, sizeof(tmp), "%03d", num); |
| 206 |
memcpy(&sbu.majorVer, &tmp, sizeof(sbu.majorVer)); |
| 207 |
} else { |
| 208 |
fprintf(stderr, "E: Unsupported coreboot major version\n"); |
| 209 |
goto out; |
| 210 |
} |
| 211 |
|
| 212 |
num = get_line_as_int(inputfilename, "COREBOOT_MINOR_VERSION", 0); |
| 213 |
if (num < 0) |
| 214 |
goto out; |
| 215 |
|
| 216 |
if (num < 99) { |
| 217 |
char tmp[3]; |
| 218 |
snprintf(tmp, sizeof(tmp), "%02d", num); |
| 219 |
memcpy(&sbu.minorVer, &tmp, sizeof(sbu.minorVer)); |
| 220 |
} else { |
| 221 |
fprintf(stderr, "E: Unsupported coreboot minor version\n"); |
| 222 |
goto out; |
| 223 |
} |
| 224 |
|
| 225 |
num = get_line_as_int(inputfilename, "COREBOOT_BUILD_YEAR_BCD", 1); |
| 226 |
if (num < 0) |
| 227 |
goto out; |
| 228 |
sbu.year = 2000 + num; |
| 229 |
|
| 230 |
num = get_line_as_int(inputfilename, "COREBOOT_BUILD_MONTH_BCD", 1); |
| 231 |
if (num < 0) |
| 232 |
goto out; |
| 233 |
sbu.month = num; |
| 234 |
|
| 235 |
num = get_line_as_int(inputfilename, "COREBOOT_BUILD_DAY_BCD", 1); |
| 236 |
if (num < 0) |
| 237 |
goto out; |
| 238 |
sbu.day = num; |
| 239 |
|
| 240 |
memcpy(&sbu.boardid[1], boardid, strlen(boardid)); |
| 241 |
|
| 242 |
|
| 243 |
FILE *fd = fopen(filename, "wb"); |
| 244 |
if (!fd) { |
| 245 |
fprintf(stderr, "E: %s open failed: %s\n", filename, strerror(errno)); |
| 246 |
goto out; |
| 247 |
} |
| 248 |
|
| 249 |
if (fwrite(&sbu, 1, sizeof(sbu), fd) != sizeof(sbu)) { |
| 250 |
fprintf(stderr, "E: %s write failed: %s\n", filename, strerror(errno)); |
| 251 |
fclose(fd); |
| 252 |
goto out; |
| 253 |
} |
| 254 |
|
| 255 |
if (fclose(fd)) { |
| 256 |
fprintf(stderr, "E: %s close failed: %s\n", filename, strerror(errno)); |
| 257 |
goto out; |
| 258 |
} |
| 259 |
|
| 260 |
ret = 0; |
| 261 |
out: |
| 262 |
if (ret > 0) |
| 263 |
fprintf(stderr, "E: Error creating '%s'\n", filename); |
| 264 |
|
| 265 |
free(filename); |
| 266 |
|
| 267 |
exit(ret); |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |