diff options
author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-15 21:04:28 +0100 |
---|---|---|
committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-24 18:05:53 +0100 |
commit | c876d3bbefe0dc00c27ca0c12d29da5874946962 (patch) | |
tree | 69f468a185a369b01998314bc3ecc19b70f4fcaa /utils/mkmpioboot | |
parent | 6c6f0757d7a902feb293be165d1490c42bc8e7ad (diff) | |
download | rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.tar.gz rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.zip |
rbutil: Merge rbutil with utils folder.
rbutil uses several components from the utils folder, and can be
considered part of utils too. Having it in a separate folder is an
arbitrary split that doesn't help anymore these days, so merge them.
This also allows other utils to easily use libtools.make without the
need to navigate to a different folder.
Change-Id: I3fc2f4de19e3e776553efb5dea5f779dfec0dc21
Diffstat (limited to 'utils/mkmpioboot')
-rw-r--r-- | utils/mkmpioboot/Makefile | 23 | ||||
-rw-r--r-- | utils/mkmpioboot/main.c | 55 | ||||
-rw-r--r-- | utils/mkmpioboot/mkmpioboot.c | 243 | ||||
-rw-r--r-- | utils/mkmpioboot/mkmpioboot.h | 46 |
4 files changed, 367 insertions, 0 deletions
diff --git a/utils/mkmpioboot/Makefile b/utils/mkmpioboot/Makefile new file mode 100644 index 0000000000..8aa2d37028 --- /dev/null +++ b/utils/mkmpioboot/Makefile | |||
@@ -0,0 +1,23 @@ | |||
1 | # __________ __ ___. | ||
2 | # Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
3 | # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
4 | # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
5 | # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
6 | # \/ \/ \/ \/ \/ | ||
7 | |||
8 | TARGET_DIR ?= $(shell pwd)/ | ||
9 | # We use the UCL code available in the Rockbox tools/ directory | ||
10 | CFLAGS += -I../../tools/ucl/include -Wall | ||
11 | |||
12 | OUTPUT = mkmpioboot | ||
13 | |||
14 | # inputs | ||
15 | LIBSOURCES = mkmpioboot.c | ||
16 | SOURCES = main.c | ||
17 | EXTRADEPS = $(LIBUCL) | ||
18 | |||
19 | include ../libtools.make | ||
20 | |||
21 | # explicit dependencies | ||
22 | $(OBJDIR)mkmpioboot.o: mkmpioboot.c mkmpioboot.h | ||
23 | |||
diff --git a/utils/mkmpioboot/main.c b/utils/mkmpioboot/main.c new file mode 100644 index 0000000000..9861cba261 --- /dev/null +++ b/utils/mkmpioboot/main.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id:$ | ||
9 | * | ||
10 | * Copyright (C) 2010 by Marcin Bukat | ||
11 | * | ||
12 | * code taken mostly from mkboot.c | ||
13 | * Copyright (C) 2005 by Linus Nielsen Feltzing | ||
14 | * | ||
15 | * This program is free software; you can redistribute it and/or | ||
16 | * modify it under the terms of the GNU General Public License | ||
17 | * as published by the Free Software Foundation; either version 2 | ||
18 | * of the License, or (at your option) any later version. | ||
19 | * | ||
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
21 | * KIND, either express or implied. | ||
22 | * | ||
23 | ****************************************************************************/ | ||
24 | |||
25 | #include <stdio.h> | ||
26 | #include <stdlib.h> | ||
27 | #include "mkmpioboot.h" | ||
28 | |||
29 | static void usage(void) | ||
30 | { | ||
31 | printf("usage: mkmpioboot <firmware file> <boot file> <output file>\n"); | ||
32 | exit(1); | ||
33 | } | ||
34 | |||
35 | int main(int argc, char *argv[]) | ||
36 | { | ||
37 | char *infile, *bootfile, *outfile; | ||
38 | int origin = 0xe0000; /* MPIO HD200 bootloader address */ | ||
39 | |||
40 | fprintf(stderr, | ||
41 | "mkmpioboot Version " VERSION "\n" | ||
42 | "This is free software; see the source for copying conditions. There is NO\n" | ||
43 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" | ||
44 | "\n"); | ||
45 | |||
46 | if(argc < 3) { | ||
47 | usage(); | ||
48 | } | ||
49 | |||
50 | infile = argv[1]; | ||
51 | bootfile = argv[2]; | ||
52 | outfile = argv[3]; | ||
53 | |||
54 | return mkmpioboot(infile, bootfile, outfile, origin); | ||
55 | } | ||
diff --git a/utils/mkmpioboot/mkmpioboot.c b/utils/mkmpioboot/mkmpioboot.c new file mode 100644 index 0000000000..ea619ed2f2 --- /dev/null +++ b/utils/mkmpioboot/mkmpioboot.c | |||
@@ -0,0 +1,243 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id:$ | ||
9 | * | ||
10 | * Copyright (C) 2010 by Marcin Bukat | ||
11 | * | ||
12 | * code taken mostly from mkboot.c | ||
13 | * Copyright (C) 2005 by Linus Nielsen Feltzing | ||
14 | * | ||
15 | * This program is free software; you can redistribute it and/or | ||
16 | * modify it under the terms of the GNU General Public License | ||
17 | * as published by the Free Software Foundation; either version 2 | ||
18 | * of the License, or (at your option) any later version. | ||
19 | * | ||
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
21 | * KIND, either express or implied. | ||
22 | * | ||
23 | ****************************************************************************/ | ||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | #include "mkmpioboot.h" | ||
28 | |||
29 | #define OF_FIRMWARE_LEN 0x100000 /* size of the OF file */ | ||
30 | #define MPIO_STRING_OFFSET 0xfffe0 /* offset of the version string in OF */ | ||
31 | #define BOOTLOADER_MAX_SIZE 0x1f800 /* free space size */ | ||
32 | |||
33 | struct mpio_model { | ||
34 | /* Descriptive name of this model */ | ||
35 | const char* model_name; | ||
36 | /* Model name used in the Rockbox header in ".mpio" files - these match the | ||
37 | -add parameter to the "scramble" tool */ | ||
38 | const char* rb_model_name; | ||
39 | /* Model number used to initialise the checksum in the Rockbox header in | ||
40 | ".mpio" files - these are the same as MODEL_NUMBER in config-target.h */ | ||
41 | const int rb_model_num; | ||
42 | /* Strings which indentifies OF version */ | ||
43 | const char* of_model_string; | ||
44 | }; | ||
45 | |||
46 | static const struct mpio_model mpio_models[] = { | ||
47 | [MODEL_HD200] = | ||
48 | { "MPIO HD200", "hd20", 69, "HD200 HDD Audio Ver113005" }, | ||
49 | [MODEL_HD300] = | ||
50 | { "MPIO HD300", "hd30", 70, "HD300 HDD Audio Ver113006" }, | ||
51 | }; | ||
52 | |||
53 | |||
54 | /* MPIO HD200 and HD300 firmware is plain binary image | ||
55 | * 4 bytes of initial SP (loaded on reset) | ||
56 | * 4 bytes of initial PC (loaded on reset) | ||
57 | * binary image with entry point 0x00000008 | ||
58 | * | ||
59 | * We put our bootloader code at 0x000e0000 | ||
60 | * and patch reset vector to jump directly | ||
61 | * into our code on reset | ||
62 | */ | ||
63 | |||
64 | static unsigned char image[OF_FIRMWARE_LEN]; | ||
65 | |||
66 | static unsigned int get_uint32be(unsigned char* p) | ||
67 | { | ||
68 | return ((p[0] << 24) | (p[1] << 16) | (p[2]<<8) | p[3]); | ||
69 | } | ||
70 | |||
71 | static long checksum(unsigned char* buf, int model, unsigned long length) | ||
72 | { | ||
73 | unsigned long chksum = model; | ||
74 | unsigned long i; | ||
75 | |||
76 | if(buf == NULL) | ||
77 | return -1; | ||
78 | |||
79 | for (i = 0; i < length; i++) | ||
80 | { | ||
81 | chksum += *buf++; | ||
82 | } | ||
83 | |||
84 | return chksum; | ||
85 | } | ||
86 | |||
87 | int mkmpioboot(const char* infile, const char* bootfile, const char* outfile, int origin) | ||
88 | { | ||
89 | FILE *f; | ||
90 | int i; | ||
91 | int len; | ||
92 | int model_index; | ||
93 | unsigned long file_checksum; | ||
94 | unsigned char header[8]; | ||
95 | |||
96 | memset(image, 0xff, sizeof(image)); | ||
97 | |||
98 | /* First, read the mpio original firmware into the image */ | ||
99 | f = fopen(infile, "rb"); | ||
100 | if(!f) | ||
101 | { | ||
102 | fprintf(stderr, "[ERR] Can not open %s file for reading\n", infile); | ||
103 | return -1; | ||
104 | } | ||
105 | |||
106 | i = fread(image, 1, OF_FIRMWARE_LEN, f); | ||
107 | if(i < OF_FIRMWARE_LEN) | ||
108 | { | ||
109 | fprintf(stderr, "[ERR] %s file read error\n", infile); | ||
110 | fclose(f); | ||
111 | return -2; | ||
112 | } | ||
113 | |||
114 | fclose(f); | ||
115 | |||
116 | /* Now check if we have OF file loaded based on presence | ||
117 | * of the version string in firmware | ||
118 | */ | ||
119 | |||
120 | for(model_index = 0; model_index < NUM_MODELS; model_index++) | ||
121 | if (strcmp(mpio_models[model_index].of_model_string, | ||
122 | (char*)(image + MPIO_STRING_OFFSET)) == 0) | ||
123 | break; | ||
124 | |||
125 | if(model_index == NUM_MODELS) | ||
126 | { | ||
127 | fprintf(stderr, "[ERR] Unknown MPIO original firmware version\n"); | ||
128 | return -3; | ||
129 | } | ||
130 | |||
131 | fprintf(stderr, "[INFO] Loading original firmware file for %s\n", | ||
132 | mpio_models[model_index].model_name); | ||
133 | |||
134 | /* Now, read the boot loader into the image */ | ||
135 | f = fopen(bootfile, "rb"); | ||
136 | if(!f) | ||
137 | { | ||
138 | fprintf(stderr, "[ERR] Can not open %s file for reading\n", bootfile); | ||
139 | return -4; | ||
140 | } | ||
141 | |||
142 | fprintf(stderr, "[INFO] Loading Rockbox bootloader file\n"); | ||
143 | |||
144 | /* get bootloader size | ||
145 | * excluding header | ||
146 | */ | ||
147 | fseek(f, 0, SEEK_END); | ||
148 | len = ftell(f) - 8; | ||
149 | |||
150 | if (len > BOOTLOADER_MAX_SIZE) | ||
151 | { | ||
152 | fprintf(stderr, "[ERR] Bootloader doesn't fit in firmware file.\n"); | ||
153 | fprintf(stderr, "[ERR] This bootloader is %d bytes long\n", len); | ||
154 | fprintf(stderr, "[ERR] and maximum allowed size is %d bytes\n", | ||
155 | BOOTLOADER_MAX_SIZE); | ||
156 | return -5; | ||
157 | } | ||
158 | |||
159 | /* Now check if the place we want to put | ||
160 | * our bootloader is free | ||
161 | */ | ||
162 | for(i=0;i<len;i++) | ||
163 | { | ||
164 | if (image[origin+i] != 0) | ||
165 | { | ||
166 | fprintf(stderr, "[ERR] Place for bootloader in OF file not empty\n"); | ||
167 | return -6; | ||
168 | } | ||
169 | } | ||
170 | |||
171 | fseek(f, 0, SEEK_SET); | ||
172 | |||
173 | /* get bootloader header*/ | ||
174 | fread(header,1,8,f); | ||
175 | |||
176 | if ( memcmp(header + 4, mpio_models[model_index].rb_model_name, 4) != 0 ) | ||
177 | { | ||
178 | fprintf(stderr, "[ERR] Original firmware and rockbox bootloader mismatch!\n"); | ||
179 | fprintf(stderr, "[ERR] Double check that you have bootloader for %s\n", | ||
180 | mpio_models[model_index].model_name); | ||
181 | return -7; | ||
182 | } | ||
183 | |||
184 | /* omit header */ | ||
185 | fseek(f, 8, SEEK_SET); | ||
186 | |||
187 | i = fread(image + origin, 1, len, f); | ||
188 | if(i < len) | ||
189 | { | ||
190 | fprintf(stderr, "[ERR] %s file read error\n", bootfile); | ||
191 | fclose(f); | ||
192 | return -8; | ||
193 | } | ||
194 | |||
195 | fclose(f); | ||
196 | |||
197 | /* calculate checksum and compare with data | ||
198 | * from header | ||
199 | */ | ||
200 | file_checksum = checksum(image + origin, mpio_models[model_index].rb_model_num, len); | ||
201 | |||
202 | if ( file_checksum != get_uint32be(header) ) | ||
203 | { | ||
204 | fprintf(stderr,"[ERR] Bootloader checksum error\n"); | ||
205 | return -9; | ||
206 | } | ||
207 | |||
208 | f = fopen(outfile, "wb"); | ||
209 | if(!f) | ||
210 | { | ||
211 | fprintf(stderr, "[ERR] Can not open %s file for writing\n" ,outfile); | ||
212 | return -10; | ||
213 | } | ||
214 | |||
215 | fprintf(stderr, "[INFO] Patching reset vector\n"); | ||
216 | |||
217 | /* Patch the stack pointer address */ | ||
218 | image[0] = image[origin + 0]; | ||
219 | image[1] = image[origin + 1]; | ||
220 | image[2] = image[origin + 2]; | ||
221 | image[3] = image[origin + 3]; | ||
222 | |||
223 | /* Patch the reset vector to start the boot loader */ | ||
224 | image[4] = image[origin + 4]; | ||
225 | image[5] = image[origin + 5]; | ||
226 | image[6] = image[origin + 6]; | ||
227 | image[7] = image[origin + 7]; | ||
228 | |||
229 | i = fwrite(image, 1, OF_FIRMWARE_LEN, f); | ||
230 | if(i < OF_FIRMWARE_LEN) | ||
231 | { | ||
232 | fprintf(stderr,"[ERR] %s file write error\n", outfile); | ||
233 | fclose(f); | ||
234 | return -11; | ||
235 | } | ||
236 | |||
237 | fprintf(stderr,"[INFO] Wrote 0x%x bytes in %s\n", OF_FIRMWARE_LEN, outfile); | ||
238 | fprintf(stderr,"[INFO] Patching succeeded!\n"); | ||
239 | |||
240 | fclose(f); | ||
241 | |||
242 | return 0; | ||
243 | } | ||
diff --git a/utils/mkmpioboot/mkmpioboot.h b/utils/mkmpioboot/mkmpioboot.h new file mode 100644 index 0000000000..4828ac953e --- /dev/null +++ b/utils/mkmpioboot/mkmpioboot.h | |||
@@ -0,0 +1,46 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id:$ | ||
9 | * | ||
10 | * Copyright (C) 2010 by Marcin Bukat | ||
11 | * code based mainly on mkboot.h | ||
12 | * Copyright (C) 2008 by Dominik Riebeling | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or | ||
15 | * modify it under the terms of the GNU General Public License | ||
16 | * as published by the Free Software Foundation; either version 2 | ||
17 | * of the License, or (at your option) any later version. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
22 | ****************************************************************************/ | ||
23 | |||
24 | #ifndef MKMPIOBOOT_H | ||
25 | #define MKMPIOBOOT_H | ||
26 | |||
27 | #ifdef __cplusplus | ||
28 | extern "C" { | ||
29 | #endif | ||
30 | |||
31 | /* Supported models */ | ||
32 | enum { | ||
33 | MODEL_UNKNOWN = -1, | ||
34 | MODEL_HD200 = 0, | ||
35 | MODEL_HD300, | ||
36 | |||
37 | NUM_MODELS | ||
38 | }; | ||
39 | |||
40 | int mkmpioboot(const char* infile, const char* bootfile, const char* outfile, int origin); | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | } | ||
44 | #endif | ||
45 | #endif | ||
46 | |||