blob: f81e5c87c73b5a070d0f29f781fadcf1f5f8aa4e [file] [log] [blame]
Simon Glasscd392fe2014-11-10 18:00:22 -07001/*
2 * ifdtool - Manage Intel Firmware Descriptor information
3 *
4 * Copyright 2014 Google, Inc
5 *
6 * SPDX-License-Identifier: GPL-2.0
7 *
8 * From Coreboot project, but it got a serious code clean-up
9 * and a few new features
10 */
11
12#include <assert.h>
13#include <fcntl.h>
14#include <getopt.h>
15#include <stdlib.h>
16#include <stdio.h>
17#include <string.h>
18#include <unistd.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include "ifdtool.h"
22
23#undef DEBUG
24
25#ifdef DEBUG
26#define debug(fmt, args...) printf(fmt, ##args)
27#else
28#define debug(fmt, args...)
29#endif
30
31#define FD_SIGNATURE 0x0FF0A55A
32#define FLREG_BASE(reg) ((reg & 0x00000fff) << 12);
33#define FLREG_LIMIT(reg) (((reg & 0x0fff0000) >> 4) | 0xfff);
34
35/**
36 * find_fd() - Find the flash description in the ROM image
37 *
38 * @image: Pointer to image
39 * @size: Size of image in bytes
40 * @return pointer to structure, or NULL if not found
41 */
42static struct fdbar_t *find_fd(char *image, int size)
43{
44 uint32_t *ptr, *end;
45
46 /* Scan for FD signature */
47 for (ptr = (uint32_t *)image, end = ptr + size / 4; ptr < end; ptr++) {
48 if (*ptr == FD_SIGNATURE)
49 break;
50 }
51
52 if (ptr == end) {
53 printf("No Flash Descriptor found in this image\n");
54 return NULL;
55 }
56
57 debug("Found Flash Descriptor signature at 0x%08x\n", i);
58
59 return (struct fdbar_t *)ptr;
60}
61
62/**
63 * get_region() - Get information about the selected region
64 *
65 * @frba: Flash region list
66 * @region_type: Type of region (0..MAX_REGIONS-1)
67 * @region: Region information is written here
68 * @return 0 if OK, else -ve
69 */
70static int get_region(struct frba_t *frba, int region_type,
71 struct region_t *region)
72{
73 if (region_type >= MAX_REGIONS) {
74 fprintf(stderr, "Invalid region type.\n");
75 return -1;
76 }
77
78 region->base = FLREG_BASE(frba->flreg[region_type]);
79 region->limit = FLREG_LIMIT(frba->flreg[region_type]);
80 region->size = region->limit - region->base + 1;
81
82 return 0;
83}
84
85static const char *region_name(int region_type)
86{
87 static const char *const regions[] = {
88 "Flash Descriptor",
89 "BIOS",
90 "Intel ME",
91 "GbE",
92 "Platform Data"
93 };
94
95 assert(region_type < MAX_REGIONS);
96
97 return regions[region_type];
98}
99
100static const char *region_filename(int region_type)
101{
102 static const char *const region_filenames[] = {
103 "flashregion_0_flashdescriptor.bin",
104 "flashregion_1_bios.bin",
105 "flashregion_2_intel_me.bin",
106 "flashregion_3_gbe.bin",
107 "flashregion_4_platform_data.bin"
108 };
109
110 assert(region_type < MAX_REGIONS);
111
112 return region_filenames[region_type];
113}
114
115static int dump_region(int num, struct frba_t *frba)
116{
117 struct region_t region;
118 int ret;
119
120 ret = get_region(frba, num, &region);
121 if (ret)
122 return ret;
123
124 printf(" Flash Region %d (%s): %08x - %08x %s\n",
125 num, region_name(num), region.base, region.limit,
126 region.size < 1 ? "(unused)" : "");
127
128 return ret;
129}
130
131static void dump_frba(struct frba_t *frba)
132{
133 int i;
134
135 printf("Found Region Section\n");
136 for (i = 0; i < MAX_REGIONS; i++) {
137 printf("FLREG%d: 0x%08x\n", i, frba->flreg[i]);
138 dump_region(i, frba);
139 }
140}
141
142static void decode_spi_frequency(unsigned int freq)
143{
144 switch (freq) {
145 case SPI_FREQUENCY_20MHZ:
146 printf("20MHz");
147 break;
148 case SPI_FREQUENCY_33MHZ:
149 printf("33MHz");
150 break;
151 case SPI_FREQUENCY_50MHZ:
152 printf("50MHz");
153 break;
154 default:
155 printf("unknown<%x>MHz", freq);
156 }
157}
158
159static void decode_component_density(unsigned int density)
160{
161 switch (density) {
162 case COMPONENT_DENSITY_512KB:
163 printf("512KiB");
164 break;
165 case COMPONENT_DENSITY_1MB:
166 printf("1MiB");
167 break;
168 case COMPONENT_DENSITY_2MB:
169 printf("2MiB");
170 break;
171 case COMPONENT_DENSITY_4MB:
172 printf("4MiB");
173 break;
174 case COMPONENT_DENSITY_8MB:
175 printf("8MiB");
176 break;
177 case COMPONENT_DENSITY_16MB:
178 printf("16MiB");
179 break;
180 default:
181 printf("unknown<%x>MiB", density);
182 }
183}
184
185static void dump_fcba(struct fcba_t *fcba)
186{
187 printf("\nFound Component Section\n");
188 printf("FLCOMP 0x%08x\n", fcba->flcomp);
189 printf(" Dual Output Fast Read Support: %ssupported\n",
190 (fcba->flcomp & (1 << 30)) ? "" : "not ");
191 printf(" Read ID/Read Status Clock Frequency: ");
192 decode_spi_frequency((fcba->flcomp >> 27) & 7);
193 printf("\n Write/Erase Clock Frequency: ");
194 decode_spi_frequency((fcba->flcomp >> 24) & 7);
195 printf("\n Fast Read Clock Frequency: ");
196 decode_spi_frequency((fcba->flcomp >> 21) & 7);
197 printf("\n Fast Read Support: %ssupported",
198 (fcba->flcomp & (1 << 20)) ? "" : "not ");
199 printf("\n Read Clock Frequency: ");
200 decode_spi_frequency((fcba->flcomp >> 17) & 7);
201 printf("\n Component 2 Density: ");
202 decode_component_density((fcba->flcomp >> 3) & 7);
203 printf("\n Component 1 Density: ");
204 decode_component_density(fcba->flcomp & 7);
205 printf("\n");
206 printf("FLILL 0x%08x\n", fcba->flill);
207 printf(" Invalid Instruction 3: 0x%02x\n",
208 (fcba->flill >> 24) & 0xff);
209 printf(" Invalid Instruction 2: 0x%02x\n",
210 (fcba->flill >> 16) & 0xff);
211 printf(" Invalid Instruction 1: 0x%02x\n",
212 (fcba->flill >> 8) & 0xff);
213 printf(" Invalid Instruction 0: 0x%02x\n",
214 fcba->flill & 0xff);
215 printf("FLPB 0x%08x\n", fcba->flpb);
216 printf(" Flash Partition Boundary Address: 0x%06x\n\n",
217 (fcba->flpb & 0xfff) << 12);
218}
219
220static void dump_fpsba(struct fpsba_t *fpsba)
221{
222 int i;
223
224 printf("Found PCH Strap Section\n");
225 for (i = 0; i < MAX_STRAPS; i++)
226 printf("PCHSTRP%-2d: 0x%08x\n", i, fpsba->pchstrp[i]);
227}
228
229static const char *get_enabled(int flag)
230{
231 return flag ? "enabled" : "disabled";
232}
233
234static void decode_flmstr(uint32_t flmstr)
235{
236 printf(" Platform Data Region Write Access: %s\n",
237 get_enabled(flmstr & (1 << 28)));
238 printf(" GbE Region Write Access: %s\n",
239 get_enabled(flmstr & (1 << 27)));
240 printf(" Intel ME Region Write Access: %s\n",
241 get_enabled(flmstr & (1 << 26)));
242 printf(" Host CPU/BIOS Region Write Access: %s\n",
243 get_enabled(flmstr & (1 << 25)));
244 printf(" Flash Descriptor Write Access: %s\n",
245 get_enabled(flmstr & (1 << 24)));
246
247 printf(" Platform Data Region Read Access: %s\n",
248 get_enabled(flmstr & (1 << 20)));
249 printf(" GbE Region Read Access: %s\n",
250 get_enabled(flmstr & (1 << 19)));
251 printf(" Intel ME Region Read Access: %s\n",
252 get_enabled(flmstr & (1 << 18)));
253 printf(" Host CPU/BIOS Region Read Access: %s\n",
254 get_enabled(flmstr & (1 << 17)));
255 printf(" Flash Descriptor Read Access: %s\n",
256 get_enabled(flmstr & (1 << 16)));
257
258 printf(" Requester ID: 0x%04x\n\n",
259 flmstr & 0xffff);
260}
261
262static void dump_fmba(struct fmba_t *fmba)
263{
264 printf("Found Master Section\n");
265 printf("FLMSTR1: 0x%08x (Host CPU/BIOS)\n", fmba->flmstr1);
266 decode_flmstr(fmba->flmstr1);
267 printf("FLMSTR2: 0x%08x (Intel ME)\n", fmba->flmstr2);
268 decode_flmstr(fmba->flmstr2);
269 printf("FLMSTR3: 0x%08x (GbE)\n", fmba->flmstr3);
270 decode_flmstr(fmba->flmstr3);
271}
272
273static void dump_fmsba(struct fmsba_t *fmsba)
274{
275 int i;
276
277 printf("Found Processor Strap Section\n");
278 for (i = 0; i < 4; i++)
279 printf("????: 0x%08x\n", fmsba->data[0]);
280}
281
282static void dump_jid(uint32_t jid)
283{
284 printf(" SPI Component Device ID 1: 0x%02x\n",
285 (jid >> 16) & 0xff);
286 printf(" SPI Component Device ID 0: 0x%02x\n",
287 (jid >> 8) & 0xff);
288 printf(" SPI Component Vendor ID: 0x%02x\n",
289 jid & 0xff);
290}
291
292static void dump_vscc(uint32_t vscc)
293{
294 printf(" Lower Erase Opcode: 0x%02x\n",
295 vscc >> 24);
296 printf(" Lower Write Enable on Write Status: 0x%02x\n",
297 vscc & (1 << 20) ? 0x06 : 0x50);
298 printf(" Lower Write Status Required: %s\n",
299 vscc & (1 << 19) ? "Yes" : "No");
300 printf(" Lower Write Granularity: %d bytes\n",
301 vscc & (1 << 18) ? 64 : 1);
302 printf(" Lower Block / Sector Erase Size: ");
303 switch ((vscc >> 16) & 0x3) {
304 case 0:
305 printf("256 Byte\n");
306 break;
307 case 1:
308 printf("4KB\n");
309 break;
310 case 2:
311 printf("8KB\n");
312 break;
313 case 3:
314 printf("64KB\n");
315 break;
316 }
317
318 printf(" Upper Erase Opcode: 0x%02x\n",
319 (vscc >> 8) & 0xff);
320 printf(" Upper Write Enable on Write Status: 0x%02x\n",
321 vscc & (1 << 4) ? 0x06 : 0x50);
322 printf(" Upper Write Status Required: %s\n",
323 vscc & (1 << 3) ? "Yes" : "No");
324 printf(" Upper Write Granularity: %d bytes\n",
325 vscc & (1 << 2) ? 64 : 1);
326 printf(" Upper Block / Sector Erase Size: ");
327 switch (vscc & 0x3) {
328 case 0:
329 printf("256 Byte\n");
330 break;
331 case 1:
332 printf("4KB\n");
333 break;
334 case 2:
335 printf("8KB\n");
336 break;
337 case 3:
338 printf("64KB\n");
339 break;
340 }
341}
342
343static void dump_vtba(struct vtba_t *vtba, int vtl)
344{
345 int i;
346 int num = (vtl >> 1) < 8 ? (vtl >> 1) : 8;
347
348 printf("ME VSCC table:\n");
349 for (i = 0; i < num; i++) {
350 printf(" JID%d: 0x%08x\n", i, vtba->entry[i].jid);
351 dump_jid(vtba->entry[i].jid);
352 printf(" VSCC%d: 0x%08x\n", i, vtba->entry[i].vscc);
353 dump_vscc(vtba->entry[i].vscc);
354 }
355 printf("\n");
356}
357
358static void dump_oem(uint8_t *oem)
359{
360 int i, j;
361 printf("OEM Section:\n");
362 for (i = 0; i < 4; i++) {
363 printf("%02x:", i << 4);
364 for (j = 0; j < 16; j++)
365 printf(" %02x", oem[(i<<4)+j]);
366 printf("\n");
367 }
368 printf("\n");
369}
370
371/**
372 * dump_fd() - Display a dump of the full flash description
373 *
374 * @image: Pointer to image
375 * @size: Size of image in bytes
376 * @return 0 if OK, -1 on error
377 */
378static int dump_fd(char *image, int size)
379{
380 struct fdbar_t *fdb = find_fd(image, size);
381
382 if (!fdb)
383 return -1;
384
385 printf("FLMAP0: 0x%08x\n", fdb->flmap0);
386 printf(" NR: %d\n", (fdb->flmap0 >> 24) & 7);
387 printf(" FRBA: 0x%x\n", ((fdb->flmap0 >> 16) & 0xff) << 4);
388 printf(" NC: %d\n", ((fdb->flmap0 >> 8) & 3) + 1);
389 printf(" FCBA: 0x%x\n", ((fdb->flmap0) & 0xff) << 4);
390
391 printf("FLMAP1: 0x%08x\n", fdb->flmap1);
392 printf(" ISL: 0x%02x\n", (fdb->flmap1 >> 24) & 0xff);
393 printf(" FPSBA: 0x%x\n", ((fdb->flmap1 >> 16) & 0xff) << 4);
394 printf(" NM: %d\n", (fdb->flmap1 >> 8) & 3);
395 printf(" FMBA: 0x%x\n", ((fdb->flmap1) & 0xff) << 4);
396
397 printf("FLMAP2: 0x%08x\n", fdb->flmap2);
398 printf(" PSL: 0x%04x\n", (fdb->flmap2 >> 8) & 0xffff);
399 printf(" FMSBA: 0x%x\n", ((fdb->flmap2) & 0xff) << 4);
400
401 printf("FLUMAP1: 0x%08x\n", fdb->flumap1);
402 printf(" Intel ME VSCC Table Length (VTL): %d\n",
403 (fdb->flumap1 >> 8) & 0xff);
404 printf(" Intel ME VSCC Table Base Address (VTBA): 0x%06x\n\n",
405 (fdb->flumap1 & 0xff) << 4);
406 dump_vtba((struct vtba_t *)
407 (image + ((fdb->flumap1 & 0xff) << 4)),
408 (fdb->flumap1 >> 8) & 0xff);
409 dump_oem((uint8_t *)image + 0xf00);
410 dump_frba((struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff)
411 << 4)));
412 dump_fcba((struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4)));
413 dump_fpsba((struct fpsba_t *)
414 (image + (((fdb->flmap1 >> 16) & 0xff) << 4)));
415 dump_fmba((struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4)));
416 dump_fmsba((struct fmsba_t *)(image + (((fdb->flmap2) & 0xff) << 4)));
417
418 return 0;
419}
420
421/**
422 * write_regions() - Write each region from an image to its own file
423 *
424 * The filename to use in each case is fixed - see region_filename()
425 *
426 * @image: Pointer to image
427 * @size: Size of image in bytes
428 * @return 0 if OK, -ve on error
429 */
430static int write_regions(char *image, int size)
431{
432 struct fdbar_t *fdb;
433 struct frba_t *frba;
434 int ret = 0;
435 int i;
436
437 fdb = find_fd(image, size);
438 if (!fdb)
439 return -1;
440
441 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
442
443 for (i = 0; i < MAX_REGIONS; i++) {
444 struct region_t region;
445 int region_fd;
446
447 ret = get_region(frba, i, &region);
448 if (ret)
449 return ret;
450 dump_region(i, frba);
451 if (region.size == 0)
452 continue;
453 region_fd = open(region_filename(i),
454 O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
455 S_IWUSR | S_IRGRP | S_IROTH);
456 if (write(region_fd, image + region.base, region.size) !=
457 region.size) {
458 perror("Error while writing");
459 ret = -1;
460 }
461 close(region_fd);
462 }
463
464 return ret;
465}
466
467/**
468 * write_image() - Write the image to a file
469 *
470 * @filename: Filename to use for the image
471 * @image: Pointer to image
472 * @size: Size of image in bytes
473 * @return 0 if OK, -ve on error
474 */
475static int write_image(char *filename, char *image, int size)
476{
477 int new_fd;
478
479 debug("Writing new image to %s\n", filename);
480
481 new_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR |
482 S_IWUSR | S_IRGRP | S_IROTH);
483 if (write(new_fd, image, size) != size) {
484 perror("Error while writing");
485 return -1;
486 }
487 close(new_fd);
488
489 return 0;
490}
491
492/**
493 * set_spi_frequency() - Set the SPI frequency to use when booting
494 *
495 * Several frequencies are supported, some of which work with fast devices.
496 * For SPI emulators, the slowest (SPI_FREQUENCY_20MHZ) is often used. The
497 * Intel boot system uses this information somehow on boot.
498 *
499 * The image is updated with the supplied value
500 *
501 * @image: Pointer to image
502 * @size: Size of image in bytes
503 * @freq: SPI frequency to use
504 */
505static void set_spi_frequency(char *image, int size, enum spi_frequency freq)
506{
507 struct fdbar_t *fdb = find_fd(image, size);
508 struct fcba_t *fcba;
509
510 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
511
512 /* clear bits 21-29 */
513 fcba->flcomp &= ~0x3fe00000;
514 /* Read ID and Read Status Clock Frequency */
515 fcba->flcomp |= freq << 27;
516 /* Write and Erase Clock Frequency */
517 fcba->flcomp |= freq << 24;
518 /* Fast Read Clock Frequency */
519 fcba->flcomp |= freq << 21;
520}
521
522/**
523 * set_em100_mode() - Set a SPI frequency that will work with Dediprog EM100
524 *
525 * @image: Pointer to image
526 * @size: Size of image in bytes
527 */
528static void set_em100_mode(char *image, int size)
529{
530 struct fdbar_t *fdb = find_fd(image, size);
531 struct fcba_t *fcba;
532
533 fcba = (struct fcba_t *)(image + (((fdb->flmap0) & 0xff) << 4));
534 fcba->flcomp &= ~(1 << 30);
535 set_spi_frequency(image, size, SPI_FREQUENCY_20MHZ);
536}
537
538/**
539 * lock_descriptor() - Lock the NE descriptor so it cannot be updated
540 *
541 * @image: Pointer to image
542 * @size: Size of image in bytes
543 */
544static void lock_descriptor(char *image, int size)
545{
546 struct fdbar_t *fdb = find_fd(image, size);
547 struct fmba_t *fmba;
548
549 /*
550 * TODO: Dynamically take Platform Data Region and GbE Region into
551 * account.
552 */
553 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
554 fmba->flmstr1 = 0x0a0b0000;
555 fmba->flmstr2 = 0x0c0d0000;
556 fmba->flmstr3 = 0x08080118;
557}
558
559/**
560 * unlock_descriptor() - Lock the NE descriptor so it can be updated
561 *
562 * @image: Pointer to image
563 * @size: Size of image in bytes
564 */
565static void unlock_descriptor(char *image, int size)
566{
567 struct fdbar_t *fdb = find_fd(image, size);
568 struct fmba_t *fmba;
569
570 fmba = (struct fmba_t *)(image + (((fdb->flmap1) & 0xff) << 4));
571 fmba->flmstr1 = 0xffff0000;
572 fmba->flmstr2 = 0xffff0000;
573 fmba->flmstr3 = 0x08080118;
574}
575
576/**
577 * open_for_read() - Open a file for reading
578 *
579 * @fname: Filename to open
580 * @sizep: Returns file size in bytes
581 * @return 0 if OK, -1 on error
582 */
583int open_for_read(const char *fname, int *sizep)
584{
585 int fd = open(fname, O_RDONLY);
586 struct stat buf;
587
588 if (fd == -1) {
589 perror("Could not open file");
590 return -1;
591 }
592 if (fstat(fd, &buf) == -1) {
593 perror("Could not stat file");
594 return -1;
595 }
596 *sizep = buf.st_size;
597 debug("File %s is %d bytes\n", fname, *sizep);
598
599 return fd;
600}
601
602/**
603 * inject_region() - Add a file to an image region
604 *
605 * This puts a file into a particular region of the flash. Several pre-defined
606 * regions are used.
607 *
608 * @image: Pointer to image
609 * @size: Size of image in bytes
610 * @region_type: Region where the file should be added
611 * @region_fname: Filename to add to the image
612 * @return 0 if OK, -ve on error
613 */
614int inject_region(char *image, int size, int region_type, char *region_fname)
615{
616 struct fdbar_t *fdb = find_fd(image, size);
617 struct region_t region;
618 struct frba_t *frba;
619 int region_size;
620 int offset = 0;
621 int region_fd;
622 int ret;
623
624 if (!fdb)
625 exit(EXIT_FAILURE);
626 frba = (struct frba_t *)(image + (((fdb->flmap0 >> 16) & 0xff) << 4));
627
628 ret = get_region(frba, region_type, &region);
629 if (ret)
630 return -1;
631 if (region.size <= 0xfff) {
632 fprintf(stderr, "Region %s is disabled in target. Not injecting.\n",
633 region_name(region_type));
634 return -1;
635 }
636
637 region_fd = open_for_read(region_fname, &region_size);
638 if (region_fd < 0)
639 return region_fd;
640
641 if ((region_size > region.size) ||
642 ((region_type != 1) && (region_size > region.size))) {
643 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Not injecting.\n",
644 region_name(region_type), region.size,
645 region.size, region_size, region_size);
646 return -1;
647 }
648
649 if ((region_type == 1) && (region_size < region.size)) {
650 fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x) bytes. Padding before injecting.\n",
651 region_name(region_type), region.size,
652 region.size, region_size, region_size);
653 offset = region.size - region_size;
654 memset(image + region.base, 0xff, offset);
655 }
656
657 if (size < region.base + offset + region_size) {
658 fprintf(stderr, "Output file is too small. (%d < %d)\n",
659 size, region.base + offset + region_size);
660 return -1;
661 }
662
663 if (read(region_fd, image + region.base + offset, region_size)
664 != region_size) {
665 perror("Could not read file");
666 return -1;
667 }
668
669 close(region_fd);
670
671 debug("Adding %s as the %s section\n", region_fname,
672 region_name(region_type));
673
674 return 0;
675}
676
677/**
678 * write_data() - Write some raw data into a region
679 *
680 * This puts a file into a particular place in the flash, ignoring the
681 * regions. Be careful not to overwrite something important.
682 *
683 * @image: Pointer to image
684 * @size: Size of image in bytes
685 * @addr: x86 ROM address to put file. The ROM ends at
686 * 0xffffffff so use an address relative to that. For an
687 * 8MB ROM the start address is 0xfff80000.
688 * @write_fname: Filename to add to the image
689 * @return 0 if OK, -ve on error
690 */
691static int write_data(char *image, int size, unsigned int addr,
692 const char *write_fname)
693{
694 int write_fd, write_size;
695 int offset;
696
697 write_fd = open_for_read(write_fname, &write_size);
698 if (write_fd < 0)
699 return write_fd;
700
701 offset = addr + size;
702 debug("Writing %s to offset %#x\n", write_fname, offset);
703
704 if (offset < 0 || offset + write_size > size) {
705 fprintf(stderr, "Output file is too small. (%d < %d)\n",
706 size, offset + write_size);
707 return -1;
708 }
709
710 if (read(write_fd, image + offset, write_size) != write_size) {
711 perror("Could not read file");
712 return -1;
713 }
714
715 close(write_fd);
716
717 return 0;
718}
719
720static void print_version(void)
721{
722 printf("ifdtool v%s -- ", IFDTOOL_VERSION);
723 printf("Copyright (C) 2014 Google Inc.\n\n");
724 printf("SPDX-License-Identifier: GPL-2.0+\n");
725}
726
727static void print_usage(const char *name)
728{
729 printf("usage: %s [-vhdix?] <filename> [<outfile>]\n", name);
730 printf("\n"
731 " -d | --dump: dump intel firmware descriptor\n"
732 " -x | --extract: extract intel fd modules\n"
733 " -i | --inject <region>:<module> inject file <module> into region <region>\n"
734 " -w | --write <addr>:<file> write file to appear at memory address <addr>\n"
Bin Meng673ed2f2014-12-12 21:05:20 +0800735 " multiple files can be written simultaneously\n"
Simon Glasscd392fe2014-11-10 18:00:22 -0700736 " -s | --spifreq <20|33|50> set the SPI frequency\n"
737 " -e | --em100 set SPI frequency to 20MHz and disable\n"
738 " Dual Output Fast Read Support\n"
739 " -l | --lock Lock firmware descriptor and ME region\n"
740 " -u | --unlock Unlock firmware descriptor and ME region\n"
741 " -r | --romsize Specify ROM size\n"
742 " -D | --write-descriptor <file> Write descriptor at base\n"
743 " -c | --create Create a new empty image\n"
744 " -v | --version: print the version\n"
745 " -h | --help: print this help\n\n"
746 "<region> is one of Descriptor, BIOS, ME, GbE, Platform\n"
747 "\n");
748}
749
750/**
751 * get_two_words() - Convert a string into two words separated by :
752 *
753 * The supplied string is split at ':', two substrings are allocated and
754 * returned.
755 *
756 * @str: String to split
757 * @firstp: Returns first string
758 * @secondp: Returns second string
759 * @return 0 if OK, -ve if @str does not have a :
760 */
761static int get_two_words(const char *str, char **firstp, char **secondp)
762{
763 const char *p;
764
765 p = strchr(str, ':');
766 if (!p)
767 return -1;
768 *firstp = strdup(str);
769 (*firstp)[p - str] = '\0';
770 *secondp = strdup(p + 1);
771
772 return 0;
773}
774
775int main(int argc, char *argv[])
776{
777 int opt, option_index = 0;
778 int mode_dump = 0, mode_extract = 0, mode_inject = 0;
779 int mode_spifreq = 0, mode_em100 = 0, mode_locked = 0;
780 int mode_unlocked = 0, mode_write = 0, mode_write_descriptor = 0;
781 int create = 0;
782 char *region_type_string = NULL, *src_fname = NULL;
783 char *addr_str = NULL;
784 int region_type = -1, inputfreq = 0;
785 enum spi_frequency spifreq = SPI_FREQUENCY_20MHZ;
Bin Meng673ed2f2014-12-12 21:05:20 +0800786 unsigned int addr[WRITE_MAX];
787 char *wr_fname[WRITE_MAX];
788 unsigned char wr_idx, wr_num = 0;
Simon Glasscd392fe2014-11-10 18:00:22 -0700789 int rom_size = -1;
790 bool write_it;
791 char *filename;
792 char *outfile = NULL;
793 struct stat buf;
794 int size = 0;
795 int bios_fd;
796 char *image;
797 int ret;
798 static struct option long_options[] = {
799 {"create", 0, NULL, 'c'},
800 {"dump", 0, NULL, 'd'},
801 {"descriptor", 1, NULL, 'D'},
802 {"em100", 0, NULL, 'e'},
803 {"extract", 0, NULL, 'x'},
804 {"inject", 1, NULL, 'i'},
805 {"lock", 0, NULL, 'l'},
806 {"romsize", 1, NULL, 'r'},
807 {"spifreq", 1, NULL, 's'},
808 {"unlock", 0, NULL, 'u'},
809 {"write", 1, NULL, 'w'},
810 {"version", 0, NULL, 'v'},
811 {"help", 0, NULL, 'h'},
812 {0, 0, 0, 0}
813 };
814
815 while ((opt = getopt_long(argc, argv, "cdD:ehi:lr:s:uvw:x?",
816 long_options, &option_index)) != EOF) {
817 switch (opt) {
818 case 'c':
819 create = 1;
820 break;
821 case 'd':
822 mode_dump = 1;
823 break;
824 case 'D':
825 mode_write_descriptor = 1;
826 src_fname = optarg;
827 break;
828 case 'e':
829 mode_em100 = 1;
830 break;
831 case 'i':
832 if (get_two_words(optarg, &region_type_string,
833 &src_fname)) {
834 print_usage(argv[0]);
835 exit(EXIT_FAILURE);
836 }
837 if (!strcasecmp("Descriptor", region_type_string))
838 region_type = 0;
839 else if (!strcasecmp("BIOS", region_type_string))
840 region_type = 1;
841 else if (!strcasecmp("ME", region_type_string))
842 region_type = 2;
843 else if (!strcasecmp("GbE", region_type_string))
844 region_type = 3;
845 else if (!strcasecmp("Platform", region_type_string))
846 region_type = 4;
847 if (region_type == -1) {
848 fprintf(stderr, "No such region type: '%s'\n\n",
849 region_type_string);
850 print_usage(argv[0]);
851 exit(EXIT_FAILURE);
852 }
853 mode_inject = 1;
854 break;
855 case 'l':
856 mode_locked = 1;
857 break;
858 case 'r':
859 rom_size = strtol(optarg, NULL, 0);
860 debug("ROM size %d\n", rom_size);
861 break;
862 case 's':
863 /* Parse the requested SPI frequency */
864 inputfreq = strtol(optarg, NULL, 0);
865 switch (inputfreq) {
866 case 20:
867 spifreq = SPI_FREQUENCY_20MHZ;
868 break;
869 case 33:
870 spifreq = SPI_FREQUENCY_33MHZ;
871 break;
872 case 50:
873 spifreq = SPI_FREQUENCY_50MHZ;
874 break;
875 default:
876 fprintf(stderr, "Invalid SPI Frequency: %d\n",
877 inputfreq);
878 print_usage(argv[0]);
879 exit(EXIT_FAILURE);
880 }
881 mode_spifreq = 1;
882 break;
883 case 'u':
884 mode_unlocked = 1;
885 break;
886 case 'v':
887 print_version();
888 exit(EXIT_SUCCESS);
889 break;
890 case 'w':
891 mode_write = 1;
Bin Meng673ed2f2014-12-12 21:05:20 +0800892 if (wr_num < WRITE_MAX) {
893 if (get_two_words(optarg, &addr_str,
894 &wr_fname[wr_num])) {
895 print_usage(argv[0]);
896 exit(EXIT_FAILURE);
897 }
898 addr[wr_num] = strtol(optarg, NULL, 0);
899 wr_num++;
900 } else {
901 fprintf(stderr,
902 "The number of files to write simultaneously exceeds the limitation (%d)\n",
903 WRITE_MAX);
Simon Glasscd392fe2014-11-10 18:00:22 -0700904 }
Simon Glasscd392fe2014-11-10 18:00:22 -0700905 break;
906 case 'x':
907 mode_extract = 1;
908 break;
909 case 'h':
910 case '?':
911 default:
912 print_usage(argv[0]);
913 exit(EXIT_SUCCESS);
914 break;
915 }
916 }
917
918 if (mode_locked == 1 && mode_unlocked == 1) {
919 fprintf(stderr, "Locking/Unlocking FD and ME are mutually exclusive\n");
920 exit(EXIT_FAILURE);
921 }
922
923 if (mode_inject == 1 && mode_write == 1) {
924 fprintf(stderr, "Inject/Write are mutually exclusive\n");
925 exit(EXIT_FAILURE);
926 }
927
928 if ((mode_dump + mode_extract + mode_inject +
929 (mode_spifreq | mode_em100 | mode_unlocked |
930 mode_locked)) > 1) {
931 fprintf(stderr, "You may not specify more than one mode.\n\n");
932 print_usage(argv[0]);
933 exit(EXIT_FAILURE);
934 }
935
936 if ((mode_dump + mode_extract + mode_inject + mode_spifreq +
937 mode_em100 + mode_locked + mode_unlocked + mode_write +
Simon Glassc03c9512014-11-12 22:42:06 -0700938 mode_write_descriptor) == 0 && !create) {
Simon Glasscd392fe2014-11-10 18:00:22 -0700939 fprintf(stderr, "You need to specify a mode.\n\n");
940 print_usage(argv[0]);
941 exit(EXIT_FAILURE);
942 }
943
944 if (create && rom_size == -1) {
945 fprintf(stderr, "You need to specify a rom size when creating.\n\n");
946 exit(EXIT_FAILURE);
947 }
948
949 if (optind + 1 != argc) {
950 fprintf(stderr, "You need to specify a file.\n\n");
951 print_usage(argv[0]);
952 exit(EXIT_FAILURE);
953 }
954
955 filename = argv[optind];
956 if (optind + 2 != argc)
957 outfile = argv[optind + 1];
958
959 if (create)
960 bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
961 else
962 bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
963
964 if (bios_fd == -1) {
965 perror("Could not open file");
966 exit(EXIT_FAILURE);
967 }
968
969 if (!create) {
970 if (fstat(bios_fd, &buf) == -1) {
971 perror("Could not stat file");
972 exit(EXIT_FAILURE);
973 }
974 size = buf.st_size;
975 }
976
977 debug("File %s is %d bytes\n", filename, size);
978
979 if (rom_size == -1)
980 rom_size = size;
981
982 image = malloc(rom_size);
983 if (!image) {
984 printf("Out of memory.\n");
985 exit(EXIT_FAILURE);
986 }
987
988 memset(image, '\xff', rom_size);
989 if (!create && read(bios_fd, image, size) != size) {
990 perror("Could not read file");
991 exit(EXIT_FAILURE);
992 }
993 if (size != rom_size) {
994 debug("ROM size changed to %d bytes\n", rom_size);
995 size = rom_size;
996 }
997
998 write_it = true;
999 ret = 0;
1000 if (mode_dump) {
1001 ret = dump_fd(image, size);
1002 write_it = false;
1003 }
1004
1005 if (mode_extract) {
1006 ret = write_regions(image, size);
1007 write_it = false;
1008 }
1009
1010 if (mode_write_descriptor)
1011 ret = write_data(image, size, -size, src_fname);
1012
1013 if (mode_inject)
1014 ret = inject_region(image, size, region_type, src_fname);
1015
Bin Meng673ed2f2014-12-12 21:05:20 +08001016 if (mode_write) {
1017 for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
1018 ret = write_data(image, size,
1019 addr[wr_idx], wr_fname[wr_idx]);
1020 if (ret)
1021 break;
1022 }
1023 }
Simon Glasscd392fe2014-11-10 18:00:22 -07001024
1025 if (mode_spifreq)
1026 set_spi_frequency(image, size, spifreq);
1027
1028 if (mode_em100)
1029 set_em100_mode(image, size);
1030
1031 if (mode_locked)
1032 lock_descriptor(image, size);
1033
1034 if (mode_unlocked)
1035 unlock_descriptor(image, size);
1036
1037 if (write_it) {
1038 if (outfile) {
1039 ret = write_image(outfile, image, size);
1040 } else {
1041 if (lseek(bios_fd, 0, SEEK_SET)) {
1042 perror("Error while seeking");
1043 ret = -1;
1044 }
1045 if (write(bios_fd, image, size) != size) {
1046 perror("Error while writing");
1047 ret = -1;
1048 }
1049 }
1050 }
1051
1052 free(image);
1053 close(bios_fd);
1054
1055 return ret ? 1 : 0;
1056}