diff options
author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2009-06-13 14:03:31 +0000 |
---|---|---|
committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2009-06-13 14:03:31 +0000 |
commit | 1c83e6ab900706a4d5d7dad631cae2305fa39f11 (patch) | |
tree | 824d1ac356737842d3ab692efa04d66faf04a9df | |
parent | ef4e8b2d81ac298844acf9f0f8b43b1e3145a939 (diff) | |
download | rockbox-1c83e6ab900706a4d5d7dad631cae2305fa39f11.tar.gz rockbox-1c83e6ab900706a4d5d7dad631cae2305fa39f11.zip |
Refactor sendfirm functionality in beastpatcher and do some code police.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21271 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | utils/MTP/beastpatcher/beastpatcher.c | 33 | ||||
-rw-r--r-- | utils/MTP/beastpatcher/beastpatcher.h | 9 | ||||
-rw-r--r-- | utils/MTP/beastpatcher/main.c | 6 | ||||
-rw-r--r-- | utils/MTP/beastpatcher/mtp_libmtp.c | 2 | ||||
-rw-r--r-- | utils/MTP/beastpatcher/mtp_win32.c | 105 |
5 files changed, 90 insertions, 65 deletions
diff --git a/utils/MTP/beastpatcher/beastpatcher.c b/utils/MTP/beastpatcher/beastpatcher.c index 05993531ff..72de1b57b8 100644 --- a/utils/MTP/beastpatcher/beastpatcher.c +++ b/utils/MTP/beastpatcher/beastpatcher.c | |||
@@ -56,7 +56,7 @@ | |||
56 | #include "bootimg.h" | 56 | #include "bootimg.h" |
57 | 57 | ||
58 | /* Code to create a single-boot bootloader. | 58 | /* Code to create a single-boot bootloader. |
59 | Based on tools/gigabeats.c by Will Robertson. | 59 | Based on tools/gigabeats.c by Will Robertson. |
60 | */ | 60 | */ |
61 | 61 | ||
62 | /* Entry point (and load address) for the main Rockbox bootloader */ | 62 | /* Entry point (and load address) for the main Rockbox bootloader */ |
@@ -83,7 +83,7 @@ static void create_single_boot(unsigned char* boot, int bootlen, | |||
83 | { | 83 | { |
84 | unsigned char* buf; | 84 | unsigned char* buf; |
85 | 85 | ||
86 | /* 15 bytes for header, 16 for signature bypass, | 86 | /* 15 bytes for header, 16 for signature bypass, |
87 | * 12 for record header, size of bootloader, 12 for footer */ | 87 | * 12 for record header, size of bootloader, 12 for footer */ |
88 | *fwsize = 15 + 16 + 12 + bootlen + 12; | 88 | *fwsize = 15 + 16 + 12 + bootlen + 12; |
89 | *fwbuf = malloc(*fwsize); | 89 | *fwbuf = malloc(*fwsize); |
@@ -106,7 +106,7 @@ static void create_single_boot(unsigned char* boot, int bootlen, | |||
106 | 106 | ||
107 | /* If the value below is too small, the update will attempt to flash. | 107 | /* If the value below is too small, the update will attempt to flash. |
108 | * Be careful when changing this (leaving it as is won't cause issues) */ | 108 | * Be careful when changing this (leaving it as is won't cause issues) */ |
109 | put_uint32le(0xCC0CD8, buf+11); | 109 | put_uint32le(0xCC0CD8, buf+11); |
110 | 110 | ||
111 | /* Step 3: Add the signature bypass record */ | 111 | /* Step 3: Add the signature bypass record */ |
112 | put_uint32le(0x88065A10, buf+15); | 112 | put_uint32le(0x88065A10, buf+15); |
@@ -186,3 +186,30 @@ int beastpatcher(void) | |||
186 | return 0; | 186 | return 0; |
187 | } | 187 | } |
188 | 188 | ||
189 | |||
190 | int sendfirm(const char* filename) | ||
191 | { | ||
192 | struct mtp_info_t mtp_info; | ||
193 | |||
194 | if (mtp_init(&mtp_info) < 0) { | ||
195 | fprintf(stderr,"[ERR] Can not init MTP\n"); | ||
196 | return 1; | ||
197 | } | ||
198 | |||
199 | /* Scan for attached MTP devices. */ | ||
200 | if (mtp_scan(&mtp_info) < 0) | ||
201 | { | ||
202 | fprintf(stderr,"[ERR] No devices found\n"); | ||
203 | return 1; | ||
204 | } | ||
205 | |||
206 | printf("[INFO] Found device \"%s - %s\"\n", mtp_info.manufacturer, | ||
207 | mtp_info.modelname); | ||
208 | printf("[INFO] Device version: \"%s\"\n",mtp_info.version); | ||
209 | |||
210 | mtp_send_file(&mtp_info, filename); | ||
211 | |||
212 | mtp_finished(&mtp_info); | ||
213 | return 0; | ||
214 | } | ||
215 | |||
diff --git a/utils/MTP/beastpatcher/beastpatcher.h b/utils/MTP/beastpatcher/beastpatcher.h index c73ae910be..d5a4eb0dce 100644 --- a/utils/MTP/beastpatcher/beastpatcher.h +++ b/utils/MTP/beastpatcher/beastpatcher.h | |||
@@ -10,19 +10,19 @@ | |||
10 | * | 10 | * |
11 | * Copyright (c) 2009, Dave Chapman | 11 | * Copyright (c) 2009, Dave Chapman |
12 | * All rights reserved. | 12 | * All rights reserved. |
13 | * | 13 | * |
14 | * Redistribution and use in source and binary forms, with or without | 14 | * Redistribution and use in source and binary forms, with or without |
15 | * modification, are permitted provided that the following conditions are | 15 | * modification, are permitted provided that the following conditions are |
16 | * met: | 16 | * met: |
17 | * | 17 | * |
18 | * * Redistributions of source code must retain the above copyright | 18 | * * Redistributions of source code must retain the above copyright |
19 | * notice, this list of conditions and the following disclaimer. | 19 | * notice, this list of conditions and the following disclaimer. |
20 | * | 20 | * |
21 | * * Redistributions in binary form must reproduce the above | 21 | * * Redistributions in binary form must reproduce the above |
22 | * copyright notice, this list of conditions and the following | 22 | * copyright notice, this list of conditions and the following |
23 | * disclaimer in the documentation and/or other materials provided | 23 | * disclaimer in the documentation and/or other materials provided |
24 | * with the distribution. | 24 | * with the distribution. |
25 | * | 25 | * |
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
@@ -41,5 +41,6 @@ | |||
41 | #define BEASTPATCHER_H | 41 | #define BEASTPATCHER_H |
42 | 42 | ||
43 | int beastpatcher(void); | 43 | int beastpatcher(void); |
44 | int sendfirm(const char* filename); | ||
44 | 45 | ||
45 | #endif | 46 | #endif |
diff --git a/utils/MTP/beastpatcher/main.c b/utils/MTP/beastpatcher/main.c index 1ccff72a79..6632b7ae8d 100644 --- a/utils/MTP/beastpatcher/main.c +++ b/utils/MTP/beastpatcher/main.c | |||
@@ -66,7 +66,6 @@ int main(int argc, char* argv[]) | |||
66 | { | 66 | { |
67 | int res; | 67 | int res; |
68 | char yesno[4]; | 68 | char yesno[4]; |
69 | struct mtp_info_t mtp_info; | ||
70 | 69 | ||
71 | fprintf(stderr,"beastpatcher v" VERSION " - (C) 2009 by the Rockbox developers\n"); | 70 | fprintf(stderr,"beastpatcher v" VERSION " - (C) 2009 by the Rockbox developers\n"); |
72 | fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n"); | 71 | fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n"); |
@@ -81,10 +80,7 @@ int main(int argc, char* argv[]) | |||
81 | } | 80 | } |
82 | } | 81 | } |
83 | else if((argc > 2) && ((strcmp(argv[1],"-s")==0) || (strcmp(argv[1],"--send")==0))) { | 82 | else if((argc > 2) && ((strcmp(argv[1],"-s")==0) || (strcmp(argv[1],"--send")==0))) { |
84 | mtp_init(&mtp_info); | 83 | res = sendfirm(argv[2]); |
85 | mtp_scan(&mtp_info); | ||
86 | res = mtp_send_file(&mtp_info, argv[2]); | ||
87 | mtp_finished(&mtp_info); | ||
88 | } | 84 | } |
89 | else { | 85 | else { |
90 | print_usage(); | 86 | print_usage(); |
diff --git a/utils/MTP/beastpatcher/mtp_libmtp.c b/utils/MTP/beastpatcher/mtp_libmtp.c index 89b374cad9..67b009b923 100644 --- a/utils/MTP/beastpatcher/mtp_libmtp.c +++ b/utils/MTP/beastpatcher/mtp_libmtp.c | |||
@@ -205,7 +205,7 @@ int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename) | |||
205 | fwfile = fopen(filename, "r"); | 205 | fwfile = fopen(filename, "r"); |
206 | if (fwfile == NULL) | 206 | if (fwfile == NULL) |
207 | { | 207 | { |
208 | fprintf(stderr,"[ERR] Could not create temporary file.\n"); | 208 | fprintf(stderr,"[ERR] Could not open file.\n"); |
209 | return -1; | 209 | return -1; |
210 | } | 210 | } |
211 | ret = mtp_send_fileptr(mtp_info, fwfile, sb.st_size); | 211 | ret = mtp_send_fileptr(mtp_info, fwfile, sb.st_size); |
diff --git a/utils/MTP/beastpatcher/mtp_win32.c b/utils/MTP/beastpatcher/mtp_win32.c index 1d6105f4fa..7a7c6db769 100644 --- a/utils/MTP/beastpatcher/mtp_win32.c +++ b/utils/MTP/beastpatcher/mtp_win32.c | |||
@@ -5,24 +5,24 @@ | |||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
7 | * \/ \/ \/ \/ \/ | 7 | * \/ \/ \/ \/ \/ |
8 | * | 8 | * |
9 | * $Id$ | 9 | * $Id$ |
10 | * | 10 | * |
11 | * Copyright (c) 2009, Dave Chapman | 11 | * Copyright (c) 2009, Dave Chapman |
12 | * All rights reserved. | 12 | * All rights reserved. |
13 | * | 13 | * |
14 | * Redistribution and use in source and binary forms, with or without | 14 | * Redistribution and use in source and binary forms, with or without |
15 | * modification, are permitted provided that the following conditions are | 15 | * modification, are permitted provided that the following conditions are |
16 | * met: | 16 | * met: |
17 | * | 17 | * |
18 | * * Redistributions of source code must retain the above copyright | 18 | * * Redistributions of source code must retain the above copyright |
19 | * notice, this list of conditions and the following disclaimer. | 19 | * notice, this list of conditions and the following disclaimer. |
20 | * | 20 | * |
21 | * * Redistributions in binary form must reproduce the above | 21 | * * Redistributions in binary form must reproduce the above |
22 | * copyright notice, this list of conditions and the following | 22 | * copyright notice, this list of conditions and the following |
23 | * disclaimer in the documentation and/or other materials provided | 23 | * disclaimer in the documentation and/or other materials provided |
24 | * with the distribution. | 24 | * with the distribution. |
25 | * | 25 | * |
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
@@ -93,7 +93,7 @@ static void callback(unsigned int progress, unsigned int max) | |||
93 | { | 93 | { |
94 | int percent = (progress * 100) / max; | 94 | int percent = (progress * 100) / max; |
95 | 95 | ||
96 | printf("[INFO] Progress: %u of %u (%d%%)\r", progress, max, percent); | 96 | printf("[INFO] Progress: %u of %u (%d%%)\r", progress, max, percent); |
97 | fflush(stdout); | 97 | fflush(stdout); |
98 | } | 98 | } |
99 | 99 | ||
@@ -101,19 +101,19 @@ static void callback(unsigned int progress, unsigned int max) | |||
101 | int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | 101 | int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, |
102 | int fwsize) | 102 | int fwsize) |
103 | { | 103 | { |
104 | HANDLE hTempFile; | 104 | HANDLE hTempFile; |
105 | DWORD dwRetVal; | 105 | DWORD dwRetVal; |
106 | DWORD dwBytesWritten; | 106 | DWORD dwBytesWritten; |
107 | UINT uRetVal; | 107 | UINT uRetVal; |
108 | TCHAR szTempName[1024]; | 108 | TCHAR szTempName[1024]; |
109 | TCHAR lpPathBuffer[1024]; | 109 | TCHAR lpPathBuffer[1024]; |
110 | BOOL fSuccess; | 110 | BOOL fSuccess; |
111 | wchar_t *tmp; | 111 | wchar_t *tmp; |
112 | int ret; | 112 | int ret; |
113 | 113 | ||
114 | (void)mtp_info; | 114 | (void)mtp_info; |
115 | 115 | ||
116 | /* Get the path for temporary files */ | 116 | /* Get the path for temporary files */ |
117 | dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer); | 117 | dwRetVal = GetTempPath(sizeof(lpPathBuffer), lpPathBuffer); |
118 | if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0)) | 118 | if (dwRetVal > sizeof(lpPathBuffer) || (dwRetVal == 0)) |
119 | { | 119 | { |
@@ -122,7 +122,7 @@ int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | |||
122 | } | 122 | } |
123 | 123 | ||
124 | /* Create the temporary file */ | 124 | /* Create the temporary file */ |
125 | uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName); | 125 | uRetVal = GetTempFileName(lpPathBuffer, TEXT("NKBIN"), 0, szTempName); |
126 | if (uRetVal == 0) | 126 | if (uRetVal == 0) |
127 | { | 127 | { |
128 | fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError()); | 128 | fprintf(stderr, "[ERR] GetTempFileName failed (%d)\n", (int)GetLastError()); |
@@ -130,28 +130,28 @@ int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | |||
130 | } | 130 | } |
131 | 131 | ||
132 | /* Now create the file */ | 132 | /* Now create the file */ |
133 | hTempFile = CreateFile((LPTSTR) szTempName, // file name | 133 | hTempFile = CreateFile((LPTSTR) szTempName, // file name |
134 | GENERIC_READ | GENERIC_WRITE, // open r-w | 134 | GENERIC_READ | GENERIC_WRITE, // open r-w |
135 | 0, // do not share | 135 | 0, // do not share |
136 | NULL, // default security | 136 | NULL, // default security |
137 | CREATE_ALWAYS, // overwrite existing | 137 | CREATE_ALWAYS, // overwrite existing |
138 | FILE_ATTRIBUTE_NORMAL,// normal file | 138 | FILE_ATTRIBUTE_NORMAL,// normal file |
139 | NULL); // no template | 139 | NULL); // no template |
140 | if (hTempFile == INVALID_HANDLE_VALUE) | 140 | if (hTempFile == INVALID_HANDLE_VALUE) |
141 | { | 141 | { |
142 | fprintf(stderr, "[ERR] Could not create %s\n", szTempName); | 142 | fprintf(stderr, "[ERR] Could not create %s\n", szTempName); |
143 | return -1; | 143 | return -1; |
144 | } | 144 | } |
145 | 145 | ||
146 | fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL); | 146 | fSuccess = WriteFile(hTempFile, fwbuf, fwsize, &dwBytesWritten, NULL); |
147 | if (!fSuccess) | 147 | if (!fSuccess) |
148 | { | 148 | { |
149 | fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError()); | 149 | fprintf(stderr, "[ERR] WriteFile failed (%d)\n", (int)GetLastError()); |
150 | return -1; | 150 | return -1; |
151 | } | 151 | } |
152 | 152 | ||
153 | fSuccess = CloseHandle (hTempFile); | 153 | fSuccess = CloseHandle (hTempFile); |
154 | if (!fSuccess) | 154 | if (!fSuccess) |
155 | { | 155 | { |
156 | fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError()); | 156 | fprintf(stderr, "[ERR] CloseHandle failed (%d)\n", (int)GetLastError()); |
157 | return -1; | 157 | return -1; |
@@ -159,17 +159,17 @@ int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | |||
159 | 159 | ||
160 | tmp = (LPWSTR)malloc(_tcslen(szTempName)*2+1); | 160 | tmp = (LPWSTR)malloc(_tcslen(szTempName)*2+1); |
161 | mbstowcs(tmp, (char*)szTempName, _tcslen(szTempName)*2+1); | 161 | mbstowcs(tmp, (char*)szTempName, _tcslen(szTempName)*2+1); |
162 | 162 | ||
163 | fprintf(stderr, "[INFO] Sending firmware...\n"); | 163 | fprintf(stderr, "[INFO] Sending firmware...\n"); |
164 | if (mtp_sendnk(tmp, fwsize, &callback)) | 164 | if (mtp_sendnk(tmp, fwsize, &callback)) |
165 | { | 165 | { |
166 | fprintf(stderr, "\n"); | 166 | fprintf(stderr, "\n"); |
167 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); | 167 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); |
168 | ret = 0; | 168 | ret = 0; |
169 | } | 169 | } |
170 | else | 170 | else |
171 | { | 171 | { |
172 | fprintf(stderr, "\n"); | 172 | fprintf(stderr, "\n"); |
173 | fprintf(stderr, "[ERR] Error occured during sending.\n"); | 173 | fprintf(stderr, "[ERR] Error occured during sending.\n"); |
174 | ret = -1; | 174 | ret = -1; |
175 | } | 175 | } |
@@ -184,10 +184,10 @@ int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | |||
184 | 184 | ||
185 | int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename) | 185 | int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename) |
186 | { | 186 | { |
187 | wchar_t *fn; | 187 | wchar_t *fn; |
188 | 188 | ||
189 | fn = (LPWSTR)malloc(strlen(filename)*2+1); | 189 | fn = (LPWSTR)malloc(strlen(filename)*2+1); |
190 | mbstowcs(fn, filename, strlen(filename)*2+1); | 190 | mbstowcs(fn, filename, strlen(filename)*2+1); |
191 | 191 | ||
192 | if (mtp_init(mtp_info) < 0) { | 192 | if (mtp_init(mtp_info) < 0) { |
193 | fprintf(stderr,"[ERR] Can not init MTP\n"); | 193 | fprintf(stderr,"[ERR] Can not init MTP\n"); |
@@ -200,32 +200,33 @@ int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename) | |||
200 | return 1; | 200 | return 1; |
201 | } | 201 | } |
202 | 202 | ||
203 | fprintf(stderr, "[INFO] Sending firmware...\n"); | 203 | fprintf(stderr, "[INFO] Sending firmware...\n"); |
204 | if (mtp_sendnk(fn, filesize(filename), &callback)) | 204 | if (mtp_sendnk(fn, filesize(filename), &callback)) |
205 | { | 205 | { |
206 | /* keep progress on screen */ | 206 | /* keep progress on screen */ |
207 | printf("\n"); | 207 | printf("\n"); |
208 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); | 208 | fprintf(stderr, "[INFO] Firmware sent successfully\n"); |
209 | return 0; | 209 | return 0; |
210 | } | 210 | } |
211 | else | 211 | else |
212 | { | 212 | { |
213 | fprintf(stderr, "[ERR] Error occured during sending.\n"); | 213 | fprintf(stderr, "[ERR] Error occured during sending.\n"); |
214 | return -1; | 214 | return -1; |
215 | } | 215 | } |
216 | mtp_finished(mtp_info); | 216 | mtp_finished(mtp_info); |
217 | } | 217 | } |
218 | 218 | ||
219 | 219 | ||
220 | static int filesize(const char* filename) | 220 | static int filesize(const char* filename) |
221 | { | 221 | { |
222 | struct _stat sb; | 222 | struct _stat sb; |
223 | int res; | 223 | int res; |
224 | 224 | ||
225 | res = _stat(filename, &sb); | 225 | res = _stat(filename, &sb); |
226 | if(res == -1) { | 226 | if(res == -1) { |
227 | fprintf(stderr, "Error getting filesize!\n"); | 227 | fprintf(stderr, "Error getting filesize!\n"); |
228 | return -1; | 228 | return -1; |
229 | } | 229 | } |
230 | return sb.st_size; | 230 | return sb.st_size; |
231 | } | 231 | } |
232 | |||