util/amdfwtool: unify return values, verify the file open

- Return an error if the specified file could not be opened. To do this
cleanly, the return value was added.
- Since there's now a unified return value, use it where it makes sense.
- Don't return an error from --help.  If you've asked for usage, it's
not an error.

Change-Id: I7c712d1e1927c2d4957b044b87ad26475b7a0e3b
Signed-off-by: Martin Roth <martinroth@chromium.org>
Reviewed-on: https://review.coreboot.org/17324
Tested-by: build bot (Jenkins)
Reviewed-by: Marc Jones <marc@marcjonesconsulting.com>
Reviewed-by: Marshall Dawson <marshalldawson3rd@gmail.com>
This commit is contained in:
Martin Roth 2016-11-08 11:22:12 -07:00 committed by Martin Roth
parent 2fe70b69c0
commit 31d95a2eb2
1 changed files with 29 additions and 14 deletions

View File

@ -442,6 +442,7 @@ static void register_fw_filename(amd_fw_type type, char filename[], int pspflag)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int c, pspflag = 0; int c, pspflag = 0;
int retval = 0;
#if PSP2 #if PSP2
int psp2flag = 0; int psp2flag = 0;
uint32_t *psp2dir; uint32_t *psp2dir;
@ -584,33 +585,42 @@ int main(int argc, char **argv)
if (*tmp != '\0') { if (*tmp != '\0') {
printf("Error: ROM size specified" printf("Error: ROM size specified"
" incorrectly (%s)\n\n", optarg); " incorrectly (%s)\n\n", optarg);
return 1; retval = 1;
} }
break; break;
case 'h': case 'h':
usage(); usage();
return 1; return 0;
default: default:
break; break;
} }
} }
if (!output) { if (!output) {
printf("Error: Output value is not specified.\n"); printf("Error: Output value is not specified.\n\n");
usage(); retval = 1;
exit(1); }
if (!rom_size) {
printf("Error: ROM Size is not specified.\n\n");
retval = 1;
} }
if (rom_size % 1024 != 0) { if (rom_size % 1024 != 0) {
printf("Error: ROM Size (%d bytes) should be a multiple of" printf("Error: ROM Size (%d bytes) should be a multiple of"
" 1024 bytes.\n", rom_size); " 1024 bytes.\n\n", rom_size);
exit(1); retval = 1;
} }
if (rom_size < MIN_ROM_KB * 1024) { if (rom_size < MIN_ROM_KB * 1024) {
printf("Error: ROM Size (%dKB) must be at least %dKB.\n", printf("Error: ROM Size (%dKB) must be at least %dKB.\n\n",
rom_size / 1024, MIN_ROM_KB); rom_size / 1024, MIN_ROM_KB);
exit(1); retval = 1;
}
if (retval) {
usage();
return retval;
} }
printf(" AMDFWTOOL Using ROM size of %dKB\n", rom_size / 1024); printf(" AMDFWTOOL Using ROM size of %dKB\n", rom_size / 1024);
@ -618,7 +628,7 @@ int main(int argc, char **argv)
rom_base_address = 0xFFFFFFFF - rom_size + 1; rom_base_address = 0xFFFFFFFF - rom_size + 1;
rom = malloc(rom_size); rom = malloc(rom_size);
if (!rom) if (!rom)
exit(1); return 1;
memset (rom, 0xFF, rom_size); memset (rom, 0xFF, rom_size);
current = AMD_ROMSIG_OFFSET; current = AMD_ROMSIG_OFFSET;
@ -679,9 +689,14 @@ int main(int argc, char **argv)
#endif #endif
targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666); targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666);
write(targetfd, amd_romsig, current - AMD_ROMSIG_OFFSET); if (targetfd >= 0) {
close(targetfd); write(targetfd, amd_romsig, current - AMD_ROMSIG_OFFSET);
free(rom); close(targetfd);
} else {
printf("Error: could not open file: %s\n", output);
retval = 1;
}
return 0; free(rom);
return retval;
} }