Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2017 Iru Cai <mytbk920423@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <assert.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | static void usage(const char *s) |
| 22 | { |
| 23 | printf("%s <rom file>\n", s); |
| 24 | exit(1); |
| 25 | } |
| 26 | |
| 27 | static void FseekEnd(FILE *fp, long o) |
| 28 | { |
| 29 | if (fseek(fp, o, SEEK_END) != 0) { |
| 30 | puts("fseek() error!\n"); |
| 31 | exit(1); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void dump_fw(FILE *dst, FILE *src, long offset) |
| 36 | { |
| 37 | static unsigned char buf[65536]; |
| 38 | |
| 39 | if (offset > 0) |
| 40 | offset -= 0x1000000; |
| 41 | |
| 42 | printf("Dumping firmware at -0x%lx...", -offset); |
| 43 | |
| 44 | FseekEnd(src, offset); |
| 45 | unsigned short len; |
| 46 | unsigned short cksum; |
| 47 | unsigned short _cksum = 0; |
| 48 | fread(&len, 2, 1, src); |
| 49 | fread(&cksum, 2, 1, src); |
| 50 | fread(buf, len, 1, src); |
| 51 | |
Iru Cai | 457fba6 | 2017-08-12 09:20:55 +0800 | [diff] [blame] | 52 | for (size_t i = 0; i < len; i++) |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 53 | _cksum += buf[i]; |
Iru Cai | 457fba6 | 2017-08-12 09:20:55 +0800 | [diff] [blame] | 54 | |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 55 | if (_cksum == cksum) { |
| 56 | puts("checksum ok"); |
| 57 | } else { |
| 58 | puts("checksum fail"); |
| 59 | exit(1); |
| 60 | } |
| 61 | |
| 62 | fwrite(&len, 2, 1, dst); |
| 63 | fwrite(&cksum, 2, 1, dst); |
| 64 | fwrite(buf, len, 1, dst); |
| 65 | } |
| 66 | |
| 67 | int main(int argc, char *argv[]) |
| 68 | { |
| 69 | if (argc != 2) |
| 70 | usage(argv[0]); |
| 71 | |
Iru Cai | 457fba6 | 2017-08-12 09:20:55 +0800 | [diff] [blame] | 72 | FILE *fp = fopen(argv[1], "rb"); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 73 | |
| 74 | if (fp == NULL) { |
| 75 | puts("Error opening file!"); |
| 76 | exit(1); |
| 77 | } |
| 78 | |
| 79 | char *basename = strrchr(argv[1], '/'); |
| 80 | if (basename == NULL) |
| 81 | basename = argv[1]; |
| 82 | else |
| 83 | basename = basename + 1; |
| 84 | |
| 85 | int len = strlen(basename); |
| 86 | char fn1[len + 5], fn2[len + 5]; |
| 87 | strcpy(fn1, basename); |
| 88 | strcpy(fn2, basename); |
| 89 | strcat(fn1, ".fw1"); |
| 90 | strcat(fn2, ".fw2"); |
| 91 | |
Iru Cai | 457fba6 | 2017-08-12 09:20:55 +0800 | [diff] [blame] | 92 | FILE *fw1 = fopen(fn1, "wb"); |
| 93 | FILE *fw2 = fopen(fn2, "wb"); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 94 | |
| 95 | long romsz; |
| 96 | FseekEnd(fp, -1); |
| 97 | romsz = ftell(fp) + 1; |
| 98 | printf("size of %s: 0x%lx\n", argv[1], romsz); |
| 99 | |
| 100 | if (romsz & 0xff) { |
| 101 | puts("The ROM size must be multiple of 0x100"); |
| 102 | exit(1); |
| 103 | } |
| 104 | |
| 105 | /* read offset of fw1 and fw2 */ |
Iru Cai | 457fba6 | 2017-08-12 09:20:55 +0800 | [diff] [blame] | 106 | unsigned char offs[8]; |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 107 | FseekEnd(fp, -0x100); |
| 108 | fread(offs, 8, 1, fp); |
| 109 | |
Iru Cai | 457fba6 | 2017-08-12 09:20:55 +0800 | [diff] [blame] | 110 | assert(offs[0] + offs[2] == 0xff); |
| 111 | assert(offs[1] + offs[3] == 0xff); |
| 112 | assert(offs[4] + offs[6] == 0xff); |
| 113 | assert(offs[5] + offs[7] == 0xff); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 114 | long offw1 = (offs[0] << 16) | (offs[1] << 8); |
| 115 | long offw2 = (offs[4] << 16) | (offs[5] << 8); |
| 116 | |
| 117 | dump_fw(fw1, fp, offw1); |
| 118 | dump_fw(fw2, fp, offw2); |
| 119 | |
| 120 | fclose(fp); |
| 121 | fclose(fw1); |
| 122 | fclose(fw2); |
| 123 | return 0; |
| 124 | } |