summaryrefslogtreecommitdiffstats
path: root/utils/rk27utils/rk27load/stage3_upload.c
diff options
context:
space:
mode:
authorMarcin Bukat <marcin.bukat@gmail.com>2011-05-30 21:10:43 +0000
committerMarcin Bukat <marcin.bukat@gmail.com>2011-05-30 21:10:43 +0000
commit8f4202db285b2139cfee6269b838733d6d2a2306 (patch)
treea362bd367c35a03928b68485df0188e637e09ea3 /utils/rk27utils/rk27load/stage3_upload.c
parent976a1699da373f01dabc9353b34aef261ebf740f (diff)
downloadrockbox-8f4202db285b2139cfee6269b838733d6d2a2306.tar.gz
rockbox-8f4202db285b2139cfee6269b838733d6d2a2306.tar.bz2
rockbox-8f4202db285b2139cfee6269b838733d6d2a2306.zip
Rockchip rk27xx utils
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29936 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/rk27utils/rk27load/stage3_upload.c')
-rw-r--r--utils/rk27utils/rk27load/stage3_upload.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/utils/rk27utils/rk27load/stage3_upload.c b/utils/rk27utils/rk27load/stage3_upload.c
new file mode 100644
index 0000000000..6f10a7c995
--- /dev/null
+++ b/utils/rk27utils/rk27load/stage3_upload.c
@@ -0,0 +1,93 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libusb.h>
+
+#include "rk27load.h"
+#include "common.h"
+#include "scramble.h"
+#include "checksum.h"
+#include "stage3_upload.h"
+
+int upload_stage3_code(libusb_device_handle *hdev, char *fn_stage3)
+{
+ FILE *f;
+ uint32_t codesize;
+ uint32_t remain;
+ uint8_t *code;
+ uint16_t send_size = 0x200;
+ uint32_t i = 0;
+ int ret, transfered;
+
+ if ((f = fopen(fn_stage3, "rb")) == NULL)
+ {
+ fprintf(stderr, "[error]: Could not open file \"%s\"\n", fn_stage3);
+ return -31;
+ }
+
+ codesize = filesize(f);
+
+ fprintf(stderr, "[stage3]: Loading user code (%d bytes)... ", codesize);
+
+ /* allocate buffer */
+ code = (uint8_t *) malloc(codesize + 0x204);
+ if (code == NULL)
+ {
+ fprintf(stderr, "\n[error]: Out of memory\n");
+ fclose(f);
+ return -32;
+ }
+
+ memset(code, 0, codesize + 0x204);
+ /* read usercode into buffer */
+ if (fread(&code[4], 1, codesize, f) != codesize)
+ {
+ fprintf(stderr, "\n[error]: I/O error\n");
+ fclose(f);
+ free(f);
+ return -33;
+ }
+ fprintf(stderr, "done\n");
+
+ fclose(f);
+
+ /* put code size at the first 4 bytes */
+ codesize += 4;
+ code[0] = codesize & 0xff;
+ code[1] = (codesize >> 8) & 0xff;
+ code[2] = (codesize >> 16) & 0xff;
+ code[3] = (codesize >> 24) & 0xff;
+
+ fprintf(stderr, "[stage3]: Uploading user code (%d bytes)... ", codesize);
+
+ remain = codesize;
+
+ while (remain > 0)
+ {
+ if (remain < 0x200)
+ send_size = remain;
+
+ ret = libusb_bulk_transfer(hdev, /* handle */
+ 1, /* EP */
+ &code[i * 0x200], /* data */
+ send_size, /* length */
+ &transfered, /* xfered */
+ USB_TIMEOUT /* timeout */
+ );
+
+ if (ret != LIBUSB_SUCCESS)
+ {
+ fprintf(stderr, "\n[error]: Bulk transfer error (%d, %d)\n", ret, i);
+ free(code);
+ return -34;
+ }
+
+ remain -= send_size;
+ i++;
+ }
+
+ fprintf(stderr,"done (sent %d blocks)\n", i);
+ return 0;
+}
+