* [PATCH 2/4] libfile: implement new helper write_file_flash()
2018-06-01 18:07 [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Uwe Kleine-König
@ 2018-06-01 18:07 ` Uwe Kleine-König
2018-06-07 20:53 ` Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 3/4] omap/am33xx_bbu: use file_write_flash() instead of its own variant Uwe Kleine-König
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Uwe Kleine-König @ 2018-06-01 18:07 UTC (permalink / raw)
To: barebox
Compared to write_file() this new function also calls erase() to be
suitable for flash devices.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
include/libfile.h | 1 +
lib/libfile.c | 33 +++++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/include/libfile.h b/include/libfile.h
index fd2fadeaa81a..2c5eef71f169 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -12,6 +12,7 @@ int read_file_2(const char *filename, size_t *size, void **outbuf,
loff_t max_size);
int write_file(const char *filename, const void *buf, size_t size);
+int write_file_flash(const char *filename, const void *buf, size_t size);
int copy_file(const char *src, const char *dst, int verbose);
diff --git a/lib/libfile.c b/lib/libfile.c
index b7db22d6940a..2d4335db7dbd 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -250,6 +250,39 @@ int write_file(const char *filename, const void *buf, size_t size)
}
EXPORT_SYMBOL(write_file);
+/**
+ * write_file_flash - write a buffer to a file backed by flash
+ * @filename: The filename to write
+ * @size: The size of the buffer
+ *
+ * Functional this is identical to write_file but calls erase() before writing.
+ *
+ * Return: 0 for success or negative error value
+ */
+int write_file_flash(const char *filename, const void *buf, size_t size)
+{
+ int fd, ret;
+
+ fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT);
+ if (fd < 0)
+ return fd;
+
+ ret = erase(fd, size, 0);
+ if (ret < 0)
+ goto out_close;
+
+ ret = write_full(fd, buf, size);
+
+out_close:
+ close(fd);
+
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+EXPORT_SYMBOL(write_file_flash);
+
/**
* copy_file - Copy a file
* @src: The source filename
--
2.17.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/4] libfile: implement new helper write_file_flash()
2018-06-01 18:07 ` [PATCH 2/4] libfile: implement new helper write_file_flash() Uwe Kleine-König
@ 2018-06-07 20:53 ` Uwe Kleine-König
2018-06-08 7:10 ` Sascha Hauer
0 siblings, 1 reply; 9+ messages in thread
From: Uwe Kleine-König @ 2018-06-07 20:53 UTC (permalink / raw)
To: barebox
Hello,
On Fri, Jun 01, 2018 at 08:07:48PM +0200, Uwe Kleine-König wrote:
> + fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT);
This doesn't work on the target machine (dove cubox) I intended to use
it on an m25p0 device. It returns -28. When dropping O_TRUNC | O_CREAT
it works though. So I suggest to either back out patches 3 and 4, or
change the line above to O_WRONLY (which also matches the function
removed in patch 4).
Best regards
Uwe
--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | http://www.pengutronix.de/ |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/4] libfile: implement new helper write_file_flash()
2018-06-07 20:53 ` Uwe Kleine-König
@ 2018-06-08 7:10 ` Sascha Hauer
2018-06-08 8:44 ` Uwe Kleine-König
0 siblings, 1 reply; 9+ messages in thread
From: Sascha Hauer @ 2018-06-08 7:10 UTC (permalink / raw)
To: Uwe Kleine-König; +Cc: barebox
On Thu, Jun 07, 2018 at 10:53:01PM +0200, Uwe Kleine-König wrote:
> Hello,
>
> On Fri, Jun 01, 2018 at 08:07:48PM +0200, Uwe Kleine-König wrote:
> > + fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT);
>
> This doesn't work on the target machine (dove cubox) I intended to use
> it on an m25p0 device. It returns -28. When dropping O_TRUNC | O_CREAT
> it works though. So I suggest to either back out patches 3 and 4, or
> change the line above to O_WRONLY (which also matches the function
> removed in patch 4).
Ok, changed to O_WRONLY only.
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 2/4] libfile: implement new helper write_file_flash()
2018-06-08 7:10 ` Sascha Hauer
@ 2018-06-08 8:44 ` Uwe Kleine-König
0 siblings, 0 replies; 9+ messages in thread
From: Uwe Kleine-König @ 2018-06-08 8:44 UTC (permalink / raw)
To: Sascha Hauer; +Cc: barebox
Hello Sascha,
On Fri, Jun 08, 2018 at 09:10:44AM +0200, Sascha Hauer wrote:
> On Thu, Jun 07, 2018 at 10:53:01PM +0200, Uwe Kleine-König wrote:
> > On Fri, Jun 01, 2018 at 08:07:48PM +0200, Uwe Kleine-König wrote:
> > > + fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT);
> >
> > This doesn't work on the target machine (dove cubox) I intended to use
> > it on an m25p0 device. It returns -28. When dropping O_TRUNC | O_CREAT
> > it works though. So I suggest to either back out patches 3 and 4, or
> > change the line above to O_WRONLY (which also matches the function
> > removed in patch 4).
>
> Ok, changed to O_WRONLY only.
Thanks
Uwe
--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | http://www.pengutronix.de/ |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 3/4] omap/am33xx_bbu: use file_write_flash() instead of its own variant
2018-06-01 18:07 [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 2/4] libfile: implement new helper write_file_flash() Uwe Kleine-König
@ 2018-06-01 18:07 ` Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 4/4] scripts/kwboot: fix image check for v0 images Uwe Kleine-König
2018-06-04 7:01 ` [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Sascha Hauer
3 siblings, 0 replies; 9+ messages in thread
From: Uwe Kleine-König @ 2018-06-01 18:07 UTC (permalink / raw)
To: barebox
There are two differences between file_write_flash() and write_image()
(which is removed in this patch):
- file_write_flash() uses write_full() vs. write() in write_image()
- file_write_flash() doesn't erase the whole device but only the needed
space
So the new code should be more robust and maybe even faster.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
arch/arm/mach-omap/am33xx_bbu_nand.c | 37 ++--------------------------
1 file changed, 2 insertions(+), 35 deletions(-)
diff --git a/arch/arm/mach-omap/am33xx_bbu_nand.c b/arch/arm/mach-omap/am33xx_bbu_nand.c
index 7785d40f1f25..9ee483855e42 100644
--- a/arch/arm/mach-omap/am33xx_bbu_nand.c
+++ b/arch/arm/mach-omap/am33xx_bbu_nand.c
@@ -28,39 +28,6 @@ struct nand_bbu_handler {
int num_devicefiles;
};
-static int write_image(const char *devfile, const void *image, size_t size)
-{
- int fd = 0;
- int ret = 0;
-
- fd = open(devfile, O_WRONLY);
- if (fd < 0) {
- pr_err("could not open %s: %s\n", devfile,
- errno_str());
- return fd;
- }
-
- ret = erase(fd, ERASE_SIZE_ALL, 0);
- if (ret < 0) {
- pr_err("could not erase %s: %s\n", devfile,
- errno_str());
- close(fd);
- return ret;
- }
-
- ret = write(fd, image, size);
- if (ret < 0) {
- pr_err("could not write to fd %s: %s\n", devfile,
- errno_str());
- close(fd);
- return ret;
- }
-
- close(fd);
-
- return 0;
-}
-
/*
* Upate given nand partitions with an image
*/
@@ -80,12 +47,12 @@ static int nand_slot_update_handler(struct bbu_handler *handler,
/* check if the devicefile has been overwritten */
if (strcmp(data->devicefile, nh->devicefile[0]) != 0) {
- ret = write_image(data->devicefile, image, size);
+ ret = write_file_flash(data->devicefile, image, size);
if (ret != 0)
return ret;
} else {
for (i = 0; i < nh->num_devicefiles; i++) {
- ret = write_image(nh->devicefile[i], image, size);
+ ret = write_file_flash(nh->devicefile[i], image, size);
if (ret != 0)
return ret;
}
--
2.17.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 4/4] scripts/kwboot: fix image check for v0 images
2018-06-01 18:07 [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 2/4] libfile: implement new helper write_file_flash() Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 3/4] omap/am33xx_bbu: use file_write_flash() instead of its own variant Uwe Kleine-König
@ 2018-06-01 18:07 ` Uwe Kleine-König
2018-06-04 7:01 ` [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Sascha Hauer
3 siblings, 0 replies; 9+ messages in thread
From: Uwe Kleine-König @ 2018-06-01 18:07 UTC (permalink / raw)
To: barebox
Since kwboot checks the contents of the file to send it only works for
v1 images (or with -f). Extend the check to know about v0 images, too.
Fixes: 39ebd7e73bec ("kwboot: do a filetype check before sending the image")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
scripts/kwboot.c | 63 ++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 53 insertions(+), 10 deletions(-)
diff --git a/scripts/kwboot.c b/scripts/kwboot.c
index db177ceb5f2e..df52144e45c4 100644
--- a/scripts/kwboot.c
+++ b/scripts/kwboot.c
@@ -602,12 +602,24 @@ out:
return rc;
}
+static unsigned char crc(const unsigned char *img, size_t len)
+{
+ unsigned char ret = 0;
+ size_t i;
+
+ for (i = 0; i < len; ++i)
+ ret += img[i];
+
+ return ret;
+}
+
static int
kwboot_check_image(unsigned char *img, size_t size)
{
size_t i;
size_t header_size, image_size, image_offset;
unsigned char csum = 0;
+ unsigned char imgversion;
if (size < 0x20) {
fprintf(stderr,
@@ -636,7 +648,8 @@ kwboot_check_image(unsigned char *img, size_t size)
return 1;
}
- if (img[0x8] != 1) {
+ imgversion = img[0x8];
+ if (imgversion > 1) {
fprintf(stderr, "Unknown version: 0x%hhx\n", img[0x8]);
return 1;
}
@@ -646,7 +659,15 @@ kwboot_check_image(unsigned char *img, size_t size)
image_offset = img[0xc] | (img[0xd] << 8) |
(img[0xe] << 16) | (img[0xf] << 24);
- header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
+ if (imgversion == 0) {
+ /* Image format 0 */
+ header_size =
+ img[0x1e] * 0x200 /* header extensions */ +
+ img[0x1d] * 0x800 /* binary header extensions */;
+ } else {
+ /* Image format 1 */
+ header_size = (img[0x9] << 16) | img[0xa] | (img[0xb] << 8);
+ }
if (header_size > image_offset) {
fprintf(stderr, "Header (%zu) expands over image start (%zu)\n",
@@ -660,16 +681,38 @@ kwboot_check_image(unsigned char *img, size_t size)
return 1;
}
- for (i = 0; i < header_size; ++i)
- csum += img[i];
- csum -= img[0x1f];
+ if (imgversion == 0) {
+ /* check Main Header */
+ csum = crc(img, 0x1f);
+ if (csum != img[0x1f]) {
+ fprintf(stderr,
+ "Main Header checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
+ img[0x1f], csum);
+ return 1;
+ }
+
+ /* check Header Extensions */
+ for (i = 0; i < img[0x1e]; ++i) {
+ csum = crc(img + 0x20 + i * 0x200, 0x1df);
+ if (csum != img[i * 0x200 + 0x1ff]) {
+ fprintf(stderr,
+ "Extension Header #%zu checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
+ i, img[i * 0x200 + 0x1ff], csum);
+ return 1;
+ }
+ }
+ } else {
+ csum = crc(img, header_size);
+ csum -= img[0x1f];
+
+ if (csum != img[0x1f]) {
+ fprintf(stderr,
+ "Checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
+ img[0x1f], csum);
+ return 1;
+ }
- if (csum != img[0x1f]) {
- fprintf(stderr,
- "Checksum mismatch: specified: 0x%02hhx, calculated: 0x%02hhx\n",
- img[0x1f], csum);
- return 1;
}
return 0;
--
2.17.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image
2018-06-01 18:07 [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Uwe Kleine-König
` (2 preceding siblings ...)
2018-06-01 18:07 ` [PATCH 4/4] scripts/kwboot: fix image check for v0 images Uwe Kleine-König
@ 2018-06-04 7:01 ` Sascha Hauer
2018-06-04 7:02 ` Uwe Kleine-König
3 siblings, 1 reply; 9+ messages in thread
From: Sascha Hauer @ 2018-06-04 7:01 UTC (permalink / raw)
To: Uwe Kleine-König; +Cc: barebox
On Fri, Jun 01, 2018 at 08:07:47PM +0200, Uwe Kleine-König wrote:
> The differences between v0 and v1 of the mvebu kwbimage are small enough
> that the function to boot such an image can be shared between both
> variants.
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
> arch/arm/mach-mvebu/kwbootimage.c | 11 +++++++++--
> common/filetype.c | 22 +++++++++++++++++-----
> include/filetype.h | 1 +
> 3 files changed, 27 insertions(+), 7 deletions(-)
Applied 2-4. This patch is the same as the first time you sent it,
right?
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image
2018-06-04 7:01 ` [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Sascha Hauer
@ 2018-06-04 7:02 ` Uwe Kleine-König
0 siblings, 0 replies; 9+ messages in thread
From: Uwe Kleine-König @ 2018-06-04 7:02 UTC (permalink / raw)
To: Sascha Hauer; +Cc: barebox
Hello Sascha,
On Mon, Jun 04, 2018 at 09:01:37AM +0200, Sascha Hauer wrote:
> On Fri, Jun 01, 2018 at 08:07:47PM +0200, Uwe Kleine-König wrote:
> > The differences between v0 and v1 of the mvebu kwbimage are small enough
> > that the function to boot such an image can be shared between both
> > variants.
> >
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > ---
> > arch/arm/mach-mvebu/kwbootimage.c | 11 +++++++++--
> > common/filetype.c | 22 +++++++++++++++++-----
> > include/filetype.h | 1 +
> > 3 files changed, 27 insertions(+), 7 deletions(-)
>
> Applied 2-4. This patch is the same as the first time you sent it,
> right?
ack, seems I forgot I already sent it separately.
Thanks
Uwe
--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | http://www.pengutronix.de/ |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 9+ messages in thread