/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2002 Linus Nielsen Feltzing * * All files in this archive are subject to the GNU General Public License. * See the file COPYING in the source tree root for full license agreement. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include "plugin.h" /**************************************************************************** * Buffer handling: * * We allocate the MP3 buffer for storing the text to be sorted, and then * search the buffer for newlines and store an array of character pointers * to the strings. * * The pointer array grows from the top of the buffer and downwards: * * |-------------| * | pointers[2] |--------| * | pointers[1] |------| | * | pointers[0] |----| | | * |-------------| | | | * | | | | | * | | | | | * | free space | | | | * | | | | | * | | | | | * |-------------| | | | * | | | | | * | line 3\0 |<---| | | * | line 2\0 |<-----| | * | line 1\0 |<-------| * |-------------| * * The advantage of this method is that we optimize the buffer usage. * * The disadvantage is that the string pointers will be loaded in reverse * order. We therefore sort the strings in reverse order as well, so we * don't have to sort an already sorted buffer. ****************************************************************************/ /*************************************************************************** * TODO: Implement a merge sort for files larger than the buffer ****************************************************************************/ PLUGIN_HEADER static struct plugin_api* rb; int buf_size; static char *filename; static int num_entries; static char **pointers; static char *stringbuffer; static char crlf[2] = "\r\n"; /* Compare function for sorting backwards */ static int compare(const void* p1, const void* p2) { char *s1 = *(char **)p1; char *s2 = *(char **)p2; return rb->strcasecmp(s2, s1); } static void sort_buffer(void) { rb->qsort(pointers, num_entries, sizeof(char *), compare); } int read_buffer(int offset) { int fd; char *buf_ptr; char *tmp_ptr; int readsize; fd = rb->open(filename, O_RDONLY); if(fd < 0) return 10 * fd - 1; /* Fill the buffer from the file */ rb->lseek(fd, offset, SEEK_SET); readsize = rb->read(fd, stringbuffer, buf_size); rb->close(fd); if(readsize < 0) return readsize * 10 - 2; /* Temporary fix until we can do merged sorting */ if(readsize == buf_size) return buf_size; /* File too big */ buf_ptr = stringbuffer; num_entries = 0; do { tmp_ptr = buf_ptr; while(*buf_ptr != '\n' && buf_ptr < (char *)pointers) { /* Terminate the string with CR... */ if(*buf_ptr == '\r') *buf_ptr = 0; buf_ptr++; } /* ...and with LF */ if(*buf_ptr == '\n') *buf_ptr = 0; else { return tmp_ptr - stringbuffer; /* Buffer is full, return the point to resume at */ } pointers--; *pointers = tmp_ptr; num_entries++; buf_ptr++; } while(buf_ptr < stringbuffer + readsize); return 0; } static int write_file(void) { char tmpfilename[MAX_PATH+1]; int fd; int i; int rc; /* Create a temporary file */ rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename); fd = rb->creat(tmpfilename); if(fd < 0) return 10 * fd - 1; /* Write the sorted strings, with appended CR/LF, to the temp file, in reverse order */ for(i = num_entries-1;i >= 0;i--) { rc = rb->write(fd, pointers[i], rb->strlen(pointers[i])); if(rc < 0) { rb->close(fd); return 10 * rc - 2; } rc = rb->write(fd, crlf, 2); if(rc < 0) { rb->close(fd); return 10 * rc - 3; } } rb->close(fd); /* Remove the original file */ rc = rb->remove(filename); if(rc < 0) { return 10 * rc - 4; } /* Replace the old file with the new */ rc = rb->rename(tmpfilename, filename); if(rc < 0) { return 10 * rc - 5; } return 0; } enum plugin_status plugin_start(struct plugin_api* api, void* parameter) { char *buf; int rc; filename = (char *)parameter; rb = api; buf = rb->plugin_get_audio_buffer(&buf_size); /* start munching memory */ stringbuffer = buf; pointers = (char **)(buf + buf_size - sizeof(int)); rb->lcd_clear_display(); rb->splash(0, "Loading..."); rc = read_buffer(0); if(rc == 0) { rb->lcd_clear_display(); rb->splash(0, "Sorting..."); sort_buffer(); rb->lcd_clear_display(); rb->splash(0, "Writing..."); rc = write_file(); if(rc < 0) { rb->lcd_clear_display(); rb->splash(HZ, "Can't write file: %d", rc); } else { rb->lcd_clear_display(); rb->splash(HZ, "Done"); } } else { if(rc < 0) { rb->lcd_clear_display(); rb->splash(HZ, "Can't read file: %d", rc); } else { rb->lcd_clear_display(); rb->splash(HZ, "The file is too big"); } } return PLUGIN_OK; } 96'>96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/***************************************************************************
*
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* $Id$
*
* Rockbox plugin copyright (C) 2009 Dave Chapman.
* Based on encryption code (C) 2009 Michael Sparmann
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
/*
This viewer plugin is for the encryption/decryption of iPod Nano
(2nd generation) firmware images using the hardware AES crypto unit
in such devices.
Encrypted images are stored with the modelname "nn2x" and extension
".ipodx" Unencrypted images use "nn2g" and ".ipod".
Heavily based on Payloads/CryptFirmware/main.c from iBugger.
*/
#include "plugin.h"
static void aes_encrypt(void* data, uint32_t size)
{
uint32_t ptr, i;
uint32_t go = 1;
PWRCONEXT &= ~0x400;
AESTYPE = 1;
AESUNKREG0 = 1;
AESUNKREG0 = 0;
AESCONTROL = 1;
AESKEYLEN = 9;
AESOUTSIZE = size;
AESAUXSIZE = 0x10;
AESINSIZE = 0x10;
AESSIZE3 = 0x10;
for (ptr = 0; ptr < (size >> 2); ptr += 4)
{
AESOUTADDR = (uint32_t)data + (ptr << 2);
AESINADDR = (uint32_t)data + (ptr << 2);
AESAUXADDR = (uint32_t)data + (ptr << 2);
if (ptr != 0)
for (i = 0; i < 4; i++)
((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
AESSTATUS = 6;
AESGO = go;
go = 3;
while ((AESSTATUS & 6) == 0);
}
AESCONTROL = 0;
PWRCONEXT |= 0x400;
}
static void aes_decrypt(void* data, uint32_t size)
{
uint32_t ptr, i;
uint32_t go = 1;
PWRCONEXT &= ~0x400;
AESTYPE = 1;
AESUNKREG0 = 1;
AESUNKREG0 = 0;
AESCONTROL = 1;
AESKEYLEN = 8;
AESOUTSIZE = size;
AESAUXSIZE = 0x10;
AESINSIZE = 0x10;
AESSIZE3 = 0x10;
for (ptr = (size >> 2) - 4; ; ptr -= 4)
{
AESOUTADDR = (uint32_t)data + (ptr << 2);
AESINADDR = (uint32_t)data + (ptr << 2);
AESAUXADDR = (uint32_t)data + (ptr << 2);
AESSTATUS = 6;
AESGO = go;
go = 3;
while ((AESSTATUS & 6) == 0);
if (ptr == 0) break;
for (i = 0; i < 4; i++)
((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
}
AESCONTROL = 0;
PWRCONEXT |= 0x400;
}
static void calc_hash(uint32_t* data, uint32_t size, uint32_t* result)
{
uint32_t ptr, i;
uint32_t ctrl = 2;
PWRCONEXT &= ~0x4;
for (ptr = 0; ptr < (size >> 2); ptr += 0x10)
{
for (i = 0; i < 0x10; i++) HASHDATAIN[i] = data[ptr + i];
HASHCTRL = ctrl;
ctrl = 0xA;
while ((HASHCTRL & 1) != 0);
}
for (i = 0; i < 5; i ++) result[i] = HASHRESULT[i];
PWRCONEXT |= 0x4;
}
static uint32_t get_uint32be(unsigned char* buf)
{
return (uint32_t)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
}
static void put_uint32be(unsigned char* buf, uint32_t x)
{
buf[0] = (x & 0xff000000) >> 24;
buf[1] = (x & 0xff0000) >> 16;
buf[2] = (x & 0xff00) >> 8;
buf[3] = x & 0xff;
}
static uint32_t calc_checksum(uint32_t sum, unsigned char* buf, int len)
{
int i;
for (i = 0; i < len ; i++) {
sum += buf[i];
}
return sum;
}
enum plugin_status plugin_start(const void* parameter)
{
int fd;
int length;
int n;
ssize_t buf_size;
uint32_t* buf;
int size;
uint32_t sum;
uint32_t hash[0x200];
char outputfilename[MAX_PATH];
fd = rb->open(parameter,O_RDONLY);
if (fd < 0) {
rb->splash(HZ*2, "Cannot open file");
return PLUGIN_ERROR;
}
length = rb->filesize(fd);
if (length < 12) {
rb->splash(HZ*2, "File too small");
return PLUGIN_ERROR;
}
/* Get the audio buffer */
buf = rb->plugin_get_audio_buffer((size_t *)&buf_size);
/* Use uncached alias for buf - equivalent to buf |= 0x40000000 */
buf += 0x10000000;
if (length > buf_size) {
rb->splash(HZ*2, "File too big");
return PLUGIN_ERROR;
}
n = rb->read(fd, buf, length);
if (n < length) {
rb->splash(HZ*2, "Cannot read file");
return PLUGIN_ERROR;
}
rb->close(fd);
size = length - 8; /* Size of firmware image */
if (calc_checksum(MODEL_NUMBER, (unsigned char*)(buf + 2), size) !=
get_uint32be((unsigned char*)buf)) {
rb->splash(HZ*2, "Bad checksum in input file");
return PLUGIN_ERROR;
}
n = rb->strlen(parameter);
if (memcmp(buf+1,"nn2g",4)==0) {
/* Encrypting - Input file should be .ipod, output file is .ipodx */
if ((n < 6) || (rb->strcmp(parameter+n-5,".ipod") != 0)) {
rb->splash(HZ*2, "Input filename must be .ipod");
return PLUGIN_ERROR;
}
if (n + 2 > MAX_PATH) {
rb->splash(HZ*2, "Filename too long");
return PLUGIN_ERROR;
}
size = (size + 0x3f) & ~0x3f; /* Pad to multiple of 64 bytes */
if (size > (length - 8)) {
rb->memset(&buf[length/4], 0, size - (length - 8));
}
rb->strlcpy(outputfilename, parameter, MAX_PATH);
outputfilename[n] = 'x';
outputfilename[n+1] = 0;
/* Everything is OK, now do the encryption */
/* 1 - Calculate hashes */
rb->memset(hash, 0, sizeof(hash));
hash[1] = 2;
hash[2] = 1;
hash[3] = 0x40;
hash[5] = size;
calc_hash(buf + 2, size, &hash[7]);
calc_hash(hash, 0x200, &hash[0x75]);
/* 2 - Do the encryption */
rb->splash(0, "Encrypting...");
aes_encrypt(buf + 2, size);
/* 3 - Update the Rockbox header */
sum = calc_checksum(MODEL_NUMBER, (unsigned char*)hash, sizeof(hash));
sum = calc_checksum(sum, (unsigned char*)(buf + 2), size);
put_uint32be((unsigned char*)buf, sum);
memcpy(buf + 1, "nn2x", 4);
/* 4 - Write to disk */
fd = rb->open(outputfilename,O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (fd < 0) {
rb->splash(HZ*2, "Could not open output file");