freemyipod r658 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r657‎ | r658 | r659 >
Date:18:28, 24 March 2011
Author:theseven
Status:new
Tags:
Comment:
libboot: Add some new functions
Modified paths:
  • /libs/boot/export/libboot.h (modified) (history)
  • /libs/boot/main.c (modified) (history)

Diff [purge]

Index: libs/boot/export/libboot.h
@@ -28,6 +28,10 @@
2929
3030
3131 int verify_rockbox_checksum(void* image, size_t size);
 32+void check_firmware(void** firmware, int* size, bool verify,
 33+ void* buf, int maxsize, bool compressed);
 34+void load_from_file(void** firmware, int* size, bool verify, const char* filename, int maxsize);
 35+void load_from_flash(void** firmware, int* size, bool verify, const char* filename, int maxsize);
3236
3337
3438 /* emCORE library identifier */
@@ -49,6 +53,9 @@
5054 struct libboot_api
5155 {
5256 typeof(verify_rockbox_checksum)* verify_rockbox_checksum;
 57+ typeof(check_firmware)* check_firmware;
 58+ typeof(load_from_file)* load_from_file;
 59+ typeof(load_from_flash)* load_from_flash;
5360 };
5461
5562
Index: libs/boot/main.c
@@ -27,7 +27,10 @@
2828
2929 static struct libboot_api apitable =
3030 {
31 - .verify_rockbox_checksum = verify_rockbox_checksum
 31+ .verify_rockbox_checksum = verify_rockbox_checksum,
 32+ .check_firmware = check_firmware,
 33+ .load_from_file = load_from_file,
 34+ .load_from_flash = load_from_flash
3235 };
3336
3437
@@ -63,3 +66,70 @@
6467 if (checksum) return -1;
6568 return 0;
6669 }
 70+
 71+void check_firmware(void** firmware, int* size, bool verify,
 72+ void* buf, int maxsize, bool compressed)
 73+{
 74+ if (compressed && maxsize)
 75+ {
 76+ void* buf2 = malloc(maxsize);
 77+ if (buf2)
 78+ {
 79+ if (!ucl_decompress(buf, *size, buf2, (uint32_t*)size))
 80+ {
 81+ free(buf);
 82+ buf = realloc(buf2, *size);
 83+ if (!buf) buf = buf2;
 84+ if (!verify || !verify_rockbox_checksum(buf, *size))
 85+ *firmware = buf;
 86+ else free(buf);
 87+ }
 88+ else
 89+ {
 90+ free(buf2);
 91+ free(buf);
 92+ }
 93+ }
 94+ else free(buf);
 95+ }
 96+ else if (!compressed)
 97+ {
 98+ if (!verify || !verify_rockbox_checksum(buf, *size)) *firmware = buf;
 99+ else free(buf);
 100+ }
 101+}
 102+
 103+void load_from_file(void** firmware, int* size, bool verify, const char* filename, int maxsize)
 104+{
 105+ int fd = file_open(filename, O_RDONLY);
 106+ if (fd > 0)
 107+ {
 108+ *size = filesize(fd);
 109+ if (*size > 0)
 110+ {
 111+ void* buf = memalign(0x10, *size);
 112+ if (buf)
 113+ {
 114+ if (read(fd, buf, *size) == *size)
 115+ check_firmware(firmware, size, verify, buf, maxsize, maxsize);
 116+ else free(buf);
 117+ }
 118+ }
 119+ close(fd);
 120+ }
 121+}
 122+
 123+void load_from_flash(void** firmware, int* size, bool verify, const char* filename, int maxsize)
 124+{
 125+ *size = bootflash_filesize(filename);
 126+ if (*size > 0)
 127+ {
 128+ void* buf = memalign(0x10, *size);
 129+ if (buf)
 130+ {
 131+ bootflash_read(filename, buf, 0, *size);
 132+ check_firmware(firmware, size, verify, buf, maxsize,
 133+ bootflash_attributes(filename) & 0x800);
 134+ }
 135+ }
 136+}