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 <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | static void usage(const char *s) |
| 20 | { |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 21 | printf("insert firmware blobs:\n\t" |
| 22 | "%s <rom file> <fw1> <fw2> <fw1 offset> <fw2 offset>\n\n", |
| 23 | s); |
| 24 | printf("set addresses of firmware blobs only:\n\t" |
| 25 | "%s <rom file> <fw1 offset> <fw2 offset>\n\n", |
| 26 | s); |
| 27 | printf( |
| 28 | "offset can be (example is put it to 0x7ffa00 when ROM is 8MB):\n" |
| 29 | "- file offset: 0x7ffa00\n" |
| 30 | "- distance to the end of file: -0x600\n" |
| 31 | "- the address when ROM is mapped to the end of memory: " |
| 32 | "0xfffffa00\n"); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 33 | exit(1); |
| 34 | } |
| 35 | |
| 36 | static void FseekEnd(FILE *fp, long o) |
| 37 | { |
| 38 | if (fseek(fp, o, SEEK_END) != 0) { |
| 39 | puts("fseek() error!\n"); |
| 40 | exit(1); |
| 41 | } |
| 42 | } |
| 43 | |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 44 | static long negoffset(long a, long romsz) |
| 45 | { |
| 46 | if (a > 0) { |
| 47 | if (a & 0x80000000) /* the address in memory, and sizeof(long) |
| 48 | is 8 */ |
| 49 | return a - 0x100000000; |
| 50 | else /* the file offset */ |
| 51 | return a - romsz; |
| 52 | } else { |
| 53 | return a; |
| 54 | } |
| 55 | } |
| 56 | |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 57 | int main(int argc, char *argv[]) |
| 58 | { |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 59 | FILE *fp, *fw1, *fw2; |
| 60 | long offset1, offset2; |
| 61 | |
| 62 | if (argc != 4 && argc != 6) |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 63 | usage(argv[0]); |
| 64 | |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 65 | fp = fopen(argv[1], "rb+"); |
| 66 | if (fp == NULL) { |
| 67 | puts("Error opening firmware image!"); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 68 | exit(1); |
| 69 | } |
| 70 | |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 71 | if (argc == 6) { |
| 72 | fw1 = fopen(argv[2], "rb"); |
| 73 | fw2 = fopen(argv[3], "rb"); |
| 74 | offset1 = strtoul(argv[4], NULL, 0); |
| 75 | offset2 = strtoul(argv[5], NULL, 0); |
| 76 | |
| 77 | if (fw1 == NULL || fw2 == NULL) { |
| 78 | puts("Error opening file!"); |
| 79 | exit(1); |
| 80 | } |
| 81 | } else { |
| 82 | fw1 = NULL; |
| 83 | fw2 = NULL; |
| 84 | offset1 = strtoul(argv[2], NULL, 0); |
| 85 | offset2 = strtoul(argv[3], NULL, 0); |
| 86 | } |
| 87 | |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 88 | if ((offset1 & 0xff) || (offset2 & 0xff)) { |
| 89 | puts("The offsets must be aligned to 0x100"); |
| 90 | exit(1); |
| 91 | } |
| 92 | |
| 93 | long romsz; |
| 94 | FseekEnd(fp, -1); |
| 95 | romsz = ftell(fp) + 1; |
| 96 | printf("size of %s: 0x%lx\n", argv[1], romsz); |
| 97 | |
| 98 | if (romsz & 0xff) { |
| 99 | puts("The ROM size must be multiple of 0x100"); |
| 100 | exit(1); |
| 101 | } |
| 102 | |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 103 | offset1 = negoffset(offset1, romsz); |
| 104 | offset2 = negoffset(offset2, romsz); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 105 | |
| 106 | /* write two offsets to $s-0x100 */ |
| 107 | char offs[8]; |
| 108 | long os; |
| 109 | os = 0x1000000 + offset1; |
| 110 | offs[0] = os >> 16; |
| 111 | offs[1] = os >> 8; |
| 112 | offs[2] = 0xff - offs[0]; |
| 113 | offs[3] = 0xff - offs[1]; |
| 114 | os = 0x1000000 + offset2; |
| 115 | offs[4] = os >> 16; |
| 116 | offs[5] = os >> 8; |
| 117 | offs[6] = 0xff - offs[4]; |
| 118 | offs[7] = 0xff - offs[5]; |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 119 | for (size_t i = 0; i < 8; i++) |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 120 | printf("%02hhx ", offs[i]); |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 121 | |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 122 | puts(""); |
| 123 | FseekEnd(fp, -0x100); |
| 124 | printf("writing to 0x%lx\n", ftell(fp)); |
| 125 | fwrite(offs, 1, 8, fp); |
| 126 | |
Iru Cai | 4225493 | 2017-07-06 00:04:47 +0800 | [diff] [blame] | 127 | if (argc == 6) { |
| 128 | /* write fw1 and fw2 */ |
| 129 | char c; |
| 130 | FseekEnd(fp, offset1); |
| 131 | printf("writing to 0x%lx\n", ftell(fp)); |
| 132 | while (fread(&c, 1, 1, fw1) == 1) |
| 133 | fwrite(&c, 1, 1, fp); |
| 134 | |
| 135 | FseekEnd(fp, offset2); |
| 136 | printf("writing to 0x%lx\n", ftell(fp)); |
| 137 | while (fread(&c, 1, 1, fw2) == 1) |
| 138 | fwrite(&c, 1, 1, fp); |
| 139 | |
| 140 | fclose(fw1); |
| 141 | fclose(fw2); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | fclose(fp); |
Iru Cai | 5fd00ce | 2017-03-26 12:05:32 +0800 | [diff] [blame] | 145 | return 0; |
| 146 | } |