Subversion Repositories freemyipod

Rev

Rev 655 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 655 Rev 658
Line 25... Line 25...
25
#include "export/libboot.h"
25
#include "export/libboot.h"
26
 
26
 
27
 
27
 
28
static struct libboot_api apitable =
28
static struct libboot_api apitable =
29
{
29
{
30
    .verify_rockbox_checksum = verify_rockbox_checksum
30
    .verify_rockbox_checksum = verify_rockbox_checksum,
-
 
31
    .check_firmware = check_firmware,
-
 
32
    .load_from_file = load_from_file,
-
 
33
    .load_from_flash = load_from_flash
31
};
34
};
32
 
35
 
33
 
36
 
34
EMCORE_LIB_HEADER(LIBBOOT_IDENTIFIER, LIBBOOT_API_VERSION, LIBBOOT_MIN_API_VERSION,
37
EMCORE_LIB_HEADER(LIBBOOT_IDENTIFIER, LIBBOOT_API_VERSION, LIBBOOT_MIN_API_VERSION,
35
                  NULL, NULL, apitable)
38
                  NULL, NULL, apitable)
Line 61... Line 64...
61
        checksum -= data[i + 8];
64
        checksum -= data[i + 8];
62
    }
65
    }
63
    if (checksum) return -1;
66
    if (checksum) return -1;
64
    return 0;
67
    return 0;
65
}
68
}
-
 
69
 
-
 
70
void check_firmware(void** firmware, int* size, bool verify,
-
 
71
                    void* buf, int maxsize, bool compressed)
-
 
72
{
-
 
73
    if (compressed && maxsize)
-
 
74
    {
-
 
75
        void* buf2 = malloc(maxsize);
-
 
76
        if (buf2)
-
 
77
        {
-
 
78
            if (!ucl_decompress(buf, *size, buf2, (uint32_t*)size))
-
 
79
            {
-
 
80
                free(buf);
-
 
81
                buf = realloc(buf2, *size);
-
 
82
                if (!buf) buf = buf2;
-
 
83
                if (!verify || !verify_rockbox_checksum(buf, *size))
-
 
84
                    *firmware = buf;
-
 
85
                else free(buf);
-
 
86
            }
-
 
87
            else
-
 
88
            {
-
 
89
                free(buf2);
-
 
90
                free(buf);
-
 
91
            }
-
 
92
        }
-
 
93
        else free(buf);
-
 
94
    }
-
 
95
    else if (!compressed)
-
 
96
    {
-
 
97
        if (!verify || !verify_rockbox_checksum(buf, *size)) *firmware = buf;
-
 
98
        else free(buf);
-
 
99
    }
-
 
100
}
-
 
101
 
-
 
102
void load_from_file(void** firmware, int* size, bool verify, const char* filename, int maxsize)
-
 
103
{
-
 
104
    int fd = file_open(filename, O_RDONLY);
-
 
105
    if (fd > 0)
-
 
106
    {
-
 
107
        *size = filesize(fd);
-
 
108
        if (*size > 0)
-
 
109
        {
-
 
110
            void* buf = memalign(0x10, *size);
-
 
111
            if (buf)
-
 
112
            {
-
 
113
                if (read(fd, buf, *size) == *size)
-
 
114
                    check_firmware(firmware, size, verify, buf, maxsize, maxsize);
-
 
115
                else free(buf);
-
 
116
            }
-
 
117
        }
-
 
118
        close(fd);
-
 
119
    }
-
 
120
}
-
 
121
 
-
 
122
void load_from_flash(void** firmware, int* size, bool verify, const char* filename, int maxsize)
-
 
123
{
-
 
124
    *size = bootflash_filesize(filename);
-
 
125
    if (*size > 0)
-
 
126
    {
-
 
127
        void* buf = memalign(0x10, *size);
-
 
128
        if (buf)
-
 
129
        {
-
 
130
            bootflash_read(filename, buf, 0, *size);
-
 
131
            check_firmware(firmware, size, verify, buf, maxsize,
-
 
132
                           bootflash_attributes(filename) & 0x800);
-
 
133
        }
-
 
134
    }
-
 
135
}