summaryrefslogtreecommitdiff
path: root/firmware/target/arm/pp/mi4-loader.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/pp/mi4-loader.c')
-rw-r--r--firmware/target/arm/pp/mi4-loader.c164
1 files changed, 152 insertions, 12 deletions
diff --git a/firmware/target/arm/pp/mi4-loader.c b/firmware/target/arm/pp/mi4-loader.c
index c465b898b4..31b0065888 100644
--- a/firmware/target/arm/pp/mi4-loader.c
+++ b/firmware/target/arm/pp/mi4-loader.c
@@ -7,6 +7,7 @@
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * 8 *
9 * Copyright (C) 2006 by Barry Wardell 9 * Copyright (C) 2006 by Barry Wardell
10 * Copyright (C) 2020 by William Wilgus [MULTIBOOT]
10 * 11 *
11 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing 12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
12 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach 13 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
@@ -28,6 +29,97 @@
28#include "loader_strerror.h" 29#include "loader_strerror.h"
29#include "crc32-mi4.h" 30#include "crc32-mi4.h"
30#include "file.h" 31#include "file.h"
32#if defined(HAVE_BOOTDATA)
33#include "system.h"
34#include "bootdata.h"
35#include "crc32.h"
36
37/* Write bootdata into location in FIRMWARE marked by magic header
38 * Assumes buffer is already loaded with the firmware image
39 * We just need to find the location and write data into the
40 * payload region along with the crc for later verification and use.
41 * Returns payload len on success,
42 * On error returns EKEY_NOT_FOUND
43 */
44int write_bootdata(unsigned char* buf, int len, unsigned int boot_volume)
45{
46 struct boot_data_t bl_boot_data;
47 struct boot_data_t *fw_boot_data = NULL;
48 int search_len = MIN(len, BOOT_DATA_SEARCH_SIZE) - sizeof(struct boot_data_t);
49 int payload_len = EKEY_NOT_FOUND;
50
51 /* search for boot data header prior to search_len */
52 for(int i = 0;i < search_len;i++)
53 {
54 fw_boot_data = (struct boot_data_t*) &buf[i];
55 if (fw_boot_data->magic[0] != BOOT_DATA_MAGIC0 ||
56 fw_boot_data->magic[1] != BOOT_DATA_MAGIC1)
57 continue;
58
59 memset(&bl_boot_data.payload, 0, BOOT_DATA_PAYLOAD_SIZE);
60 bl_boot_data.boot_volume = boot_volume;
61
62 memset(fw_boot_data->payload, 0, fw_boot_data->length);
63 /* determine maximum bytes we can write to firmware
64 BOOT_DATA_PAYLOAD_SIZE is the size the bootloader expects */
65 payload_len = MIN(BOOT_DATA_PAYLOAD_SIZE, fw_boot_data->length);
66 fw_boot_data->length = payload_len;
67 /* copy data to FIRMWARE bootdata struct */
68 memcpy(fw_boot_data->payload, &bl_boot_data.payload, payload_len);
69 /* crc will be used within the firmware to check validity of bootdata */
70 fw_boot_data->crc = crc_32(fw_boot_data->payload, payload_len, 0xffffffff);
71 break;
72
73 }
74 return payload_len;
75}
76#endif /* HAVE_BOOTDATA */
77
78#ifdef HAVE_MULTIBOOT /* defined by config.h */
79/* Check in root of this <volume> for rockbox_main.<playername>
80 * if this file empty or there is a single slash '/'
81 * buf = '<volume#>/<rootdir>/<firmware(name)>\0'
82 * If instead '/<*DIRECTORY*>' is supplied
83 * addpath will be set to this DIRECTORY buf =
84 * '/<volume#>/addpath/<rootdir>/<firmware(name)>\0'
85 * On error returns Negative number or 0
86 * On success returns bytes from snprintf
87 * and generated path will be placed in buf
88 * note: if supplied buffer is too small return will be
89 * the number of bytes that would have been written
90 */
91int get_redirect_dir(char* buf, int buffer_size, int volume,
92 const char* rootdir, const char* firmware)
93{
94 int fd;
95 int f_offset;
96 char add_path[MAX_PATH];
97 /* Check in root of volume for rockbox_main.<playername> redirect */
98 snprintf(add_path, sizeof(add_path), "/<%d>/"BOOT_REDIR, volume);
99 fd = open(add_path, O_RDONLY);
100 if (fd < 0)
101 return EFILE_NOT_FOUND;
102
103 /*clear add_path for re-use*/
104 memset(add_path, 0, sizeof(add_path));
105 f_offset = read(fd, add_path,sizeof(add_path));
106 close(fd);
107
108 for(int i = f_offset - 1;i > 0; i--)
109 {
110 /* strip control chars < SPACE or all if path doesn't start with '/' */
111 if (add_path[i] < 0x20 || add_path[0] != '/')
112 add_path[i] = '\0';
113 }
114 /* if '/add_path' is specified in rockbox_main.<playername>
115 path is /<vol#>/add_path/rootdir/firmwarename
116 if add_path is empty or '/' is missing from beginning
117 path is /<vol#>/rootdir/firmwarename
118 */
119 return snprintf(buf, buffer_size, "/<%d>%s/%s/%s", volume, add_path,
120 rootdir, firmware);
121}
122#endif /* HAVE_MULTIBOOT */
31 123
32static inline unsigned int le2int(unsigned char* buf) 124static inline unsigned int le2int(unsigned char* buf)
33{ 125{
@@ -169,26 +261,20 @@ static int tea_find_key(struct mi4header_t *mi4header, unsigned char* buf)
169 return key_found; 261 return key_found;
170} 262}
171 263
172/* Load mi4 format firmware image */ 264/* Load mi4 format firmware image from a FULLY QUALIFIED PATH */
173int load_mi4(unsigned char* buf, 265static int load_mi4_filename(unsigned char* buf,
174 const char* firmware, 266 const char* filename,
175 unsigned int buffer_size) 267 unsigned int buffer_size)
176{ 268{
177 int fd; 269 int fd;
178 struct mi4header_t mi4header; 270 struct mi4header_t mi4header;
179 int rc; 271 int rc;
180 unsigned long sum; 272 unsigned long sum;
181 char filename[MAX_PATH];
182 273
183 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
184 fd = open(filename, O_RDONLY); 274 fd = open(filename, O_RDONLY);
275
185 if(fd < 0) 276 if(fd < 0)
186 { 277 return EFILE_NOT_FOUND;
187 snprintf(filename,sizeof(filename),"/%s",firmware);
188 fd = open(filename, O_RDONLY);
189 if(fd < 0)
190 return EFILE_NOT_FOUND;
191 }
192 278
193 read(fd, &mi4header, MI4_HEADER_SIZE); 279 read(fd, &mi4header, MI4_HEADER_SIZE);
194 280
@@ -235,3 +321,57 @@ int load_mi4(unsigned char* buf,
235 321
236 return mi4header.mi4size - MI4_HEADER_SIZE; 322 return mi4header.mi4size - MI4_HEADER_SIZE;
237} 323}
324
325/* Load mi4 format firmware image */
326int load_mi4(unsigned char* buf, const char* firmware, unsigned int buffer_size)
327{
328
329 int ret = EFILE_NOT_FOUND;
330 char filename[MAX_PATH+2];
331 /* only filename passed */
332 if (firmware[0] != '/')
333 {
334
335#ifdef HAVE_MULTIBOOT /* defined by config.h */
336 /* checks <volumes> highest index to lowest for redirect file
337 * 0 is the default boot volume, it is not checked here
338 * if found <volume>/rockbox_main.<playername> and firmware
339 * has a bootdata region this firmware will be loaded */
340 for (unsigned int i = NUM_VOLUMES - 1; i > 0 && ret < 0; i--)
341 {
342 if (get_redirect_dir(filename, sizeof(filename), i,
343 BOOTDIR, firmware) > 0)
344 {
345 ret = load_mi4_filename(buf, filename, buffer_size);
346 /* if firmware has no boot_data don't load from external drive */
347 if (write_bootdata(buf, ret, i) <= 0)
348 ret = EKEY_NOT_FOUND;
349 }
350 /* if ret is valid breaks from loop to continue loading */
351 }
352#endif
353
354 if (ret < 0) /* Check default volume, no valid firmware file loaded yet */
355 {
356 /* First check in BOOTDIR */
357 snprintf(filename, sizeof(filename), BOOTDIR "/%s",firmware);
358
359 ret = load_mi4_filename(buf, filename, buffer_size);
360
361 if (ret < 0)
362 {
363 /* Check in root dir */
364 snprintf(filename, sizeof(filename),"/%s",firmware);
365 ret = load_mi4_filename(buf, filename, buffer_size);
366 }
367#ifdef HAVE_BOOTDATA
368 /* 0 is the default boot volume */
369 write_bootdata(buf, ret, 0);
370#endif
371 }
372 }
373 else /* full path passed ROLO etc.*/
374 ret = load_mi4_filename(buf, firmware, buffer_size);
375
376 return ret;
377}