From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 12/34] scripts: imx-usb-loader: fully read images into memory
Date: Tue, 2 Feb 2016 15:47:55 +0100 [thread overview]
Message-ID: <1454424497-7157-13-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1454424497-7157-1-git-send-email-s.hauer@pengutronix.de>
imx-usb-loader tries to safe memory by reading the image in chunks.
This is unnecessarily complicated. The images are small, so fully read
them into memory and store them in a single buffer. This makes handling
them a lot easier.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
scripts/imx/imx-usb-loader.c | 246 +++++++++++++------------------------------
1 file changed, 75 insertions(+), 171 deletions(-)
diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index 5a94cf1..d3de05f 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -750,81 +750,42 @@ static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_
}
static int verify_memory(struct libusb_device_handle *h, struct usb_id *p_id,
- FILE *xfile, unsigned offset, unsigned addr, unsigned size,
- unsigned char *verify_buffer, unsigned verify_cnt)
+ const void *buf, unsigned len, unsigned addr)
{
- int mismatch = 0;
- unsigned char file_buf[1024];
- fseek(xfile, offset + verify_cnt, SEEK_SET);
-
- while (size) {
- unsigned char mem_buf[64];
- unsigned char *p = file_buf;
- int cnt = addr & 0x3f;
- int request = get_min(size, sizeof(file_buf));
-
- if (cnt) {
- cnt = 64 - cnt;
- if (request > cnt)
- request = cnt;
- }
-
- if (verify_cnt) {
- p = verify_buffer;
- cnt = get_min(request, verify_cnt);
- verify_buffer += cnt;
- verify_cnt -= cnt;
- } else {
- cnt = fread(p, 1, request, xfile);
- if (cnt <= 0) {
- printf("Unexpected end of file, request=0x%0x, size=0x%x, cnt=%i\n",
- request, size, cnt);
- return -1;
- }
- }
-
- size -= cnt;
-
- while (cnt) {
- int ret;
-
- request = get_min(cnt, sizeof(mem_buf));
+ int ret, mismatch = 0;
+ void *readbuf;
+ unsigned offset = 0, now;
- ret = read_memory(h, p_id, addr, mem_buf, request);
- if (ret < 0)
- return ret;
+ readbuf = malloc(len);
+ if (!readbuf)
+ return -ENOMEM;
- if (memcmp(p, mem_buf, request)) {
- unsigned char * m = mem_buf;
- if (!mismatch)
- printf("!!!!mismatch\n");
- mismatch++;
-
- while (request) {
- unsigned req = get_min(request, 32);
- if (memcmp(p, m, req)) {
- dump_long(p, req, offset);
- dump_long(m, req, addr);
- printf("\n");
- }
- p += req;
- m+= req;
- offset += req;
- addr += req;
- cnt -= req;
- request -= req;
- }
- if (mismatch >= 5)
- return -1;
- }
- p += request;
- offset += request;
- addr += request;
- cnt -= request;
+ ret = read_memory(h, p_id, addr, readbuf, len);
+ if (ret < 0)
+ goto err;
+
+ while (len) {
+ now = get_min(len, 32);
+
+ if (memcmp(buf + offset, readbuf + offset, now)) {
+ printf("mismatch at offset 0x%08x. expected:\n", offset);
+ dump_long(buf + offset, now, addr + offset);
+ printf("read:\n");
+ dump_long(readbuf + offset, now, addr + offset);
+ ret = -EINVAL;
+ mismatch++;
+ if (mismatch > 4)
+ goto err;
}
+
+ len -= now;
+ offset += now;
}
- return mismatch ? -1 : 0;
+err:
+ free(readbuf);
+
+ return ret;
}
static int is_header(struct usb_id *p_id, unsigned char *p)
@@ -998,8 +959,7 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id,
}
static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
- unsigned char *p, int cnt, unsigned char *buf, unsigned buf_cnt,
- unsigned dladdr, unsigned fsize, unsigned char type, FILE* xfile)
+ void *buf, unsigned len, unsigned dladdr, unsigned char type)
{
static unsigned char dl_command[] = {
0x04,
@@ -1013,18 +973,19 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
int last_trans, err;
int retry = 0;
unsigned transfer_size = 0;
- int max = p_id->mach_id->max_transfer;
unsigned char tmp[64];
+ void *p;
+ int cnt;
dl_command[2] = (unsigned char)(dladdr >> 24);
dl_command[3] = (unsigned char)(dladdr >> 16);
dl_command[4] = (unsigned char)(dladdr >> 8);
dl_command[5] = (unsigned char)(dladdr);
- dl_command[7] = (unsigned char)(fsize >> 24);
- dl_command[8] = (unsigned char)(fsize >> 16);
- dl_command[9] = (unsigned char)(fsize >> 8);
- dl_command[10] = (unsigned char)(fsize);
+ dl_command[7] = (unsigned char)(len >> 24);
+ dl_command[8] = (unsigned char)(len >> 16);
+ dl_command[9] = (unsigned char)(len >> 8);
+ dl_command[10] = (unsigned char)(len);
dl_command[15] = type;
for (;;) {
@@ -1048,61 +1009,23 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id,
err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
}
- while (1) {
- int retry;
+ p = buf;
+ cnt = len;
- if (cnt > (int)(fsize - transfer_size))
- cnt = (fsize - transfer_size);
+ while (1) {
+ int now = get_min(cnt, p_id->mach_id->max_transfer);
- if (cnt <= 0)
+ if (!now)
break;
- retry = 0;
-
- while (cnt) {
- err = transfer(h, 2, p, get_min(cnt, max), &last_trans, p_id);
- if (err) {
- printf("out err=%i, last_trans=%i cnt=0x%x max=0x%x transfer_size=0x%X retry=%i\n",
- err, last_trans, cnt, max, transfer_size, retry);
- if (retry >= 10) {
- printf("Giving up\n");
- return err;
- }
- if (max >= 16)
- max >>= 1;
- else
- max <<= 1;
- usleep(10000);
- retry++;
- continue;
- }
- max = p_id->mach_id->max_transfer;
- retry = 0;
- if (cnt < last_trans) {
- printf("error: last_trans=0x%x, attempted only=0%x\n", last_trans, cnt);
- cnt = last_trans;
- }
- if (!last_trans) {
- printf("Nothing last_trans, err=%i\n", err);
- break;
- }
- p += last_trans;
- cnt -= last_trans;
- transfer_size += last_trans;
+ err = transfer(h, 2, p, now, &now, p_id);
+ if (err) {
+ printf("dl_command err=%i, last_trans=%i\n", err, last_trans);
+ return err;
}
- if (!last_trans)
- break;
-
- if (feof(xfile))
- break;
-
- cnt = fsize - transfer_size;
- if (cnt <= 0)
- break;
-
- cnt = fread(buf, 1 , get_min(cnt, buf_cnt), xfile);
- p = buf;
+ p += now;
+ cnt -= now;
}
if (p_id->mach_id->mode == MODE_HID) {
@@ -1134,10 +1057,9 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
int cnt;
unsigned file_base;
int last_trans, err;
-#define BUF_SIZE (1024*16)
unsigned char *buf = NULL;
+ unsigned char *image;
unsigned char *verify_buffer = NULL;
- unsigned verify_cnt;
unsigned char *p;
unsigned char tmp[64];
unsigned dladdr = 0;
@@ -1146,7 +1068,6 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
unsigned header_addr = 0;
unsigned skip = 0;
- unsigned transfer_size=0;
int retry = 0;
xfile = fopen(curr->filename, "rb" );
@@ -1155,23 +1076,26 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
return -5;
}
- buf = malloc(BUF_SIZE);
- if (!buf) {
- printf("error, out of memory\n");
+ fsize = get_file_size(xfile);
+ if (fsize < 0x20) {
+ printf("error, file: %s is too small\n", curr->filename);
ret = -2;
goto cleanup;
}
- fsize = get_file_size(xfile);
-
- cnt = fread(buf, 1 , BUF_SIZE, xfile);
-
- if (cnt < 0x20) {
- printf("error, file: %s is too small\n", curr->filename);
+ buf = malloc(fsize);
+ if (!buf) {
+ printf("error, out of memory\n");
ret = -2;
goto cleanup;
}
+ cnt = fread(buf, 1 , fsize, xfile);
+ if (cnt < fsize) {
+ printf("error, cannot read %s\n", curr->filename);
+ return -1;
+ }
+
max_length = fsize;
ret = process_header(h, p_id, curr, buf, cnt,
@@ -1215,22 +1139,9 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
skip = dladdr - file_base;
- if (skip > cnt) {
- if (skip > fsize) {
- printf("skip(0x%08x) > fsize(0x%08x) file_base=0x%08x, header_offset=0x%x\n",
- skip, fsize, file_base, header_offset);
- ret = -4;
- goto cleanup;
- }
-
- fseek(xfile, skip, SEEK_SET);
- cnt -= skip;
- fsize -= skip;
- skip = 0;
- cnt = fread(buf, 1 , BUF_SIZE, xfile);
- }
+ image = buf + skip;
- p = &buf[skip];
+ p = image;
cnt -= skip;
fsize -= skip;
@@ -1238,13 +1149,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
fsize = max_length;
if (verify) {
- /*
- * we need to save header for verification
- * because some of the file is changed
- * before download
- */
- verify_buffer = malloc(cnt);
- verify_cnt = cnt;
+ verify_buffer = malloc(64);
if (!verify_buffer) {
printf("error, out of memory\n");
@@ -1252,7 +1157,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
goto cleanup;
}
- memcpy(verify_buffer, p, cnt);
+ memcpy(verify_buffer, p, 64);
if ((type == FT_APP) && (p_id->mach_id->mode != MODE_HID)) {
type = FT_LOAD_ONLY;
@@ -1263,19 +1168,16 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
printf("loading binary file(%s) to %08x, skip=0x%x, fsize=%u type=%d...\n",
curr->filename, dladdr, skip, fsize, type);
- ret = load_file(h, p_id, p, cnt, buf, BUF_SIZE,
- dladdr, fsize, type, xfile);
+ ret = load_file(h, p_id, image, fsize, dladdr, type);
if (ret < 0)
goto cleanup;
printf("binary file successfully loaded\n");
- transfer_size = ret;
-
if (verify) {
printf("verifying file...\n");
- ret = verify_memory(h, p_id, xfile, skip, dladdr, fsize, verify_buffer, verify_cnt);
+ ret = verify_memory(h, p_id, image, fsize, dladdr);
if (ret < 0) {
printf("verifying failed\n");
goto cleanup;
@@ -1284,11 +1186,13 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
printf("file successfully verified\n");
if (verify == 2) {
- if (verify_cnt > 64)
- verify_cnt = 64;
- ret = load_file(h, p_id, verify_buffer, verify_cnt,
- buf, BUF_SIZE, dladdr, verify_cnt,
- FT_APP, xfile);
+ /*
+ * In bulk mode we do not have an explicit jump command,
+ * so we load part of the image again with type FT_APP
+ * this time.
+ */
+ ret = load_file(h, p_id, verify_buffer, 64,
+ dladdr, FT_APP);
if (ret < 0)
goto cleanup;
@@ -1327,7 +1231,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id,
err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]);
}
- ret = (fsize == transfer_size) ? 0 : -16;
+ ret = 0;
cleanup:
fclose(xfile);
free(verify_buffer);
--
2.7.0.rc3
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2016-02-02 14:48 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-02 14:47 [PATCH v2] i.MX HABv4 rework and HABv3 support Sascha Hauer
2016-02-02 14:47 ` [PATCH 01/34] scripts: Add common header files for tools Sascha Hauer
2016-02-02 14:47 ` [PATCH 02/34] scripts/include: Add ARRAY_SIZE Sascha Hauer
2016-02-02 14:47 ` [PATCH 03/34] scripts: Add scripts/include to host compiler includes Sascha Hauer
2016-02-02 14:47 ` [PATCH 04/34] scripts: imx: Use Kernel includes Sascha Hauer
2016-02-02 14:47 ` [PATCH 05/34] scripts: mxs: " Sascha Hauer
2016-02-02 14:47 ` [PATCH 06/34] ARM: i.MX: Add HABv3 Kconfig variables Sascha Hauer
2016-02-02 14:47 ` [PATCH 07/34] imx: hab: rename driver dir to hab/ Sascha Hauer
2016-02-02 14:47 ` [PATCH 08/34] hab: Add HABv3 status report function Sascha Hauer
2016-02-02 14:47 ` [PATCH 09/34] scripts: imx-usb-loader: Make readonly arguments const Sascha Hauer
2016-02-02 14:47 ` [PATCH 10/34] scripts: imx-usb-loader: Move definitions up Sascha Hauer
2016-02-02 14:47 ` [PATCH 11/34] scripts: imx-image: Allow dcd offset 0x0 Sascha Hauer
2016-02-02 14:47 ` Sascha Hauer [this message]
2016-02-02 14:47 ` [PATCH 13/34] scripts: imx-usb-loader: Move load_file up Sascha Hauer
2016-02-02 14:47 ` [PATCH 14/34] scripts: imx: Consolidate flash headers in imx tools Sascha Hauer
2016-02-02 14:47 ` [PATCH 15/34] scripts: imx-image: Add context struct to config parsers Sascha Hauer
2016-02-02 14:47 ` [PATCH 16/34] scripts: imx-image: move write_mem to context data Sascha Hauer
2016-02-02 14:48 ` [PATCH 17/34] scripts: imx-image: move check " Sascha Hauer
2016-02-02 14:48 ` [PATCH 18/34] scripts: imx: move config file parser to separate file Sascha Hauer
2016-02-02 14:48 ` [PATCH 19/34] scripts: imx: make libusb variables global Sascha Hauer
2016-02-02 14:48 ` [PATCH 20/34] scripts: imx-usb-loader: Add -s and -i options Sascha Hauer
2016-02-02 14:48 ` [PATCH 21/34] scripts: imx: Drop double check Sascha Hauer
2016-02-02 14:48 ` [PATCH 22/34] scripts: imx-image: move more variables to context data Sascha Hauer
2016-02-02 14:48 ` [PATCH 23/34] scripts: imx-image: pass config data to add_header_* Sascha Hauer
2016-02-02 14:48 ` [PATCH 24/34] scripts: imx-image: Support adding a Super Root Key to the image Sascha Hauer
2016-02-02 14:48 ` [PATCH 25/34] scripts: imx: Create CSF files from imx config file Sascha Hauer
2016-02-02 14:48 ` [PATCH 26/34] scripts: imx: Allow to create signed images Sascha Hauer
2016-02-02 14:48 ` [PATCH 27/34] scripts: imx: Generate signed images with imx-image Sascha Hauer
2016-02-02 14:48 ` [PATCH 28/34] scripts: imx-usb-loader: Use dcd len to invalidate dcd data Sascha Hauer
2016-02-02 14:48 ` [PATCH 29/34] scripts: imx-image: Factor out a read_file function Sascha Hauer
2016-02-02 14:48 ` [PATCH 30/34] scripts: imx-image: Allow to create HAB signed images suitable for USB upload Sascha Hauer
2016-02-02 14:48 ` [PATCH 31/34] Make: i.MX: Allow to pass config file to cmd_imx_image Sascha Hauer
2016-02-02 14:48 ` [PATCH 32/34] images: imx: Add targets for signed images and signed usb images Sascha Hauer
2016-02-02 14:48 ` [PATCH 33/34] scripts: imx-usb-loader: Do not zero out boot_data_ptr Sascha Hauer
2016-02-02 14:48 ` [PATCH 34/34] imx: hab: Make hab status functions SoC specific Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1454424497-7157-13-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox