i.MX USB Loader Windows Fixes
Subsystems (3)
cover i.MX USB Loader Windows Fixes
Hi all,
this small series enable our imx-usb-loader and barebox-pbl implementation to get loaded via Windows.
Regards,
Marco
Marco Felsch (4):
scripts: common: fix read_file_2 for windows
scripts: common: fix buffer freeing
usb: gadget: fsl_udc: lower state_complete constraints
imx-usb-loader: fix windows usage
drivers/usb/gadget/udc/fsl_udc_pbl.c | 2 +- scripts/common.c | 5 +++-- scripts/imx/imx-usb-loader.c | 8 ++++++-- 3 files changed, 10 insertions(+), 5 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/4 scripts: common: fix read_file_2 for windows
From: Marco Felsch <[email protected]>
We need to specify the O_BINARY flag for Windows else the followed read() may detected a EOF condition.
--- scripts/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/common.c +2 -1
@@ -11,6 +11,7 @@#include <stdarg.h> #include "common.h" +#include "compiler.h" int read_file_2 (const char *filename,size_t *size,void **outbuf,size_t max_size) {
@@ -22,7 +23,7 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si *size =0 ; *outbuf =NULL ; - fd =open (filename,O_RDONLY ); + fd =open (filename,O_RDONLY |O_BINARY );if (fd <0 ) {fprintf (stderr,"Cannot open %s: %s\n" , filename,strerror (errno));return -errno;
Signature · 1 line
2.41.0
PATCH 2/4 scripts: common: fix buffer freeing
In reply to Marco FelschWe pass the wrong address to free in case of an error occured in preceding while-read loop if the loop is executed more than once. Fix this by use the outbuf which stores the original malloc'ed address.
--- scripts/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/common.c +1 -1
@@ -76,8 +76,8 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_si ret =0 ; goto close; free: +free (*outbuf); *outbuf =NULL ; -free (buf); close:close (fd);return ret;
Signature · 1 line
2.41.0
PATCH 3/4 usb: gadget: fsl_udc: lower state_complete constraints
In reply to Marco FelschSignal state_complete if the host send more data than specified first. This may become crucial with different host usb loader tools and different host OSes.
--- drivers/usb/gadget/udc/fsl_udc_pbl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/fsl_udc_pbl.c +1 -1
@@ -136,7 +136,7 @@ static void dtd_complete_irq(struct usb_dr_device *dr) actual += len -1 ; to_transfer -= len -1 ; -if (to_transfer ==0 ) +if (to_transfer <=0 ) state = state_complete; }
Signature · 1 line
2.41.0
PATCH 4/4 imx-usb-loader: fix windows usage
In reply to Marco FelschThis ports commit 561f0377db5e ("Transfer always specified report 2 length") from imx_usb_loader[1] to our implementation.
| commit 561f0377db5e1a36f5ab5e17f97d774f9f1e22ab | Author: Stefan Agner <[email protected]> | Date: Wed Nov 2 16:51:24 2016 -0700 | | Transfer always specified report 2 length | | The Windows HIDAPI is very strict when it comes to packet lengths: | The API always expect the length specified by the device and rejects | the transfer otherwise. The report size is specified by the HID | Report Descriptor. The i.MX 6 SoC reports a report 2 size of 1024. | Currently, imx_usb reads the length from the device specific config | files and stores it in max_transfer. This change uses max_transfer | for report 2 when using the HID API. | | The boot ROM handles too long transfers just fine. Also NXP's | sb_loader transfers always complete reports. The change has been | successfully tested on Linux and Windows. | | Given that the device reports the size, it is probably also "the | right thing to do"... | | With that change, we might end up transferring up to 1023 bytes | more than actually necessary. Make sure that DoIRomDownload does | not print an error message and returns successfully even if too | many bytes have been transferred (this now typically happens at the | end of every file). | | In write_dcd use a signed length variable to keep track of remaining | bytes. Continue transfer loop only if more than 0 bytes are left to | transfer. Also drop the m_ prefix, m_ is usually used for member | variables but this is a local variable. | | Signed-off-by: Stefan Agner <[email protected]>
[1] https://github.com/boundarydevices/imx_usb_loader/commit/561f0377db5e1a36f5ab5e17f97d774f9f1e22ab
--- scripts/imx/imx-usb-loader.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/imx/imx-usb-loader.c +6 -2
@@ -484,12 +484,16 @@ static int transfer(int report, void *p, unsigned cnt, int *last_trans) err =libusb_bulk_transfer (usb_dev_handle, (report <3 ) ?1 :2 +EP_IN , p, cnt, last_trans,1000 ); }else { -unsigned char tmp[1028 ]; +unsigned char tmp[1028 ] = {0 }; tmp[0 ] = (unsigned char )report;if (report <3 ) {memcpy (&tmp[1 ], p, cnt); + +if (report ==2 ) + cnt = mach_id->max_transfer; +if (mach_id->hid_endpoint) {int trans; err = libusb_interrupt_transfer(usb_dev_handle,
@@ -739,7 +743,7 @@ static int send_buf(void *buf, unsigned len)while (1 ) {int now =get_min (cnt, mach_id->max_transfer); -if (!now) +if (now <=0 )break ; err =transfer (2 , p, now, &now);
Signature · 1 line
2.41.0
Re: [PATCH 4/4] imx-usb-loader: fix windows usage
Quoted 52 lines · Marco Felsch
This ports commit
561f0377db5e("Transfer always specified report 2 length") from imx_usb_loader[1] to our implementation.| commit 561f0377db5e1a36f5ab5e17f97d774f9f1e22ab | Author: Stefan Agner <[email protected]> | Date: Wed Nov 2 16:51:24 2016 -0700 | | Transfer always specified report 2 length | | The Windows HIDAPI is very strict when it comes to packet lengths: | The API always expect the length specified by the device and rejects | the transfer otherwise. The report size is specified by the HID | Report Descriptor. The i.MX 6 SoC reports a report 2 size of 1024. | Currently, imx_usb reads the length from the device specific config | files and stores it in max_transfer. This change uses max_transfer | for report 2 when using the HID API. | | The boot ROM handles too long transfers just fine. Also NXP's | sb_loader transfers always complete reports. The change has been | successfully tested on Linux and Windows. | | Given that the device reports the size, it is probably also "the | right thing to do"... | | With that change, we might end up transferring up to 1023 bytes | more than actually necessary. Make sure that DoIRomDownload does | not print an error message and returns successfully even if too | many bytes have been transferred (this now typically happens at the | end of every file). | | In write_dcd use a signed length variable to keep track of remaining | bytes. Continue transfer loop only if more than 0 bytes are left to | transfer. Also drop the m_ prefix, m_ is usually used for member | variables but this is a local variable. | | Signed-off-by: Stefan Agner <[email protected]>[1] https://github.com/boundarydevices/imx_usb_loader/commit/561f0377db5e1a36f5ab5e17f97d774f9f1e22ab
Signed-off-by Marco Felsch <[email protected]>--- scripts/imx/imx-usb-loader.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)scripts/imx/imx-usb-loader.c +0
-0@@ -484,12 +484,16 @@ static int transfer(int report, void *p, unsigned cnt, int *last_trans) err =libusb_bulk_transfer (usb_dev_handle, (report <3 ) ?1 :2 +EP_IN , p, cnt, last_trans,1000 ); }else {
scripts/imx/imx-usb-loader.c static int transfer(int report, void *p, unsigned cnt, int *last_trans) · line 495
Quoted 10 lines
scripts/imx/imx-usb-loader.c +4
-1-unsigned char tmp[1028 ]; +unsigned char tmp[1028 ] = {0 }; tmp[0 ] = (unsigned char )report;if (report <3 ) {memcpy (&tmp[1 ], p, cnt); + +if (report ==2 ) + cnt = mach_id->max_transfer;
Did you verify that this can't result in an out-of-bounds read?
I'd have expected either an adjustment to tmp's size or a description why a buffer overread can't happen in the commit message.
Quoted 13 lines
scripts/imx/imx-usb-loader.c +2
-1+if (mach_id->hid_endpoint) {int trans; err = libusb_interrupt_transfer(usb_dev_handle,@@ -739,7 +743,7 @@ static int send_buf(void *buf, unsigned len)while (1 ) {int now =get_min (cnt, mach_id->max_transfer); -if (!now) +if (now <=0 )break ; err =transfer (2 , p, now, &now);
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH 4/4] imx-usb-loader: fix windows usage
Quoted 58 lines · Ahmad Fatoum
Quoted 57 lines · Marco Felsch
This ports commit
561f0377db5e("Transfer always specified report 2 length") from imx_usb_loader[1] to our implementation.| commit 561f0377db5e1a36f5ab5e17f97d774f9f1e22ab | Author: Stefan Agner <[email protected]> | Date: Wed Nov 2 16:51:24 2016 -0700 | | Transfer always specified report 2 length | | The Windows HIDAPI is very strict when it comes to packet lengths: | The API always expect the length specified by the device and rejects | the transfer otherwise. The report size is specified by the HID | Report Descriptor. The i.MX 6 SoC reports a report 2 size of 1024. | Currently, imx_usb reads the length from the device specific config | files and stores it in max_transfer. This change uses max_transfer | for report 2 when using the HID API. | | The boot ROM handles too long transfers just fine. Also NXP's | sb_loader transfers always complete reports. The change has been | successfully tested on Linux and Windows. | | Given that the device reports the size, it is probably also "the | right thing to do"... | | With that change, we might end up transferring up to 1023 bytes | more than actually necessary. Make sure that DoIRomDownload does | not print an error message and returns successfully even if too | many bytes have been transferred (this now typically happens at the | end of every file). | | In write_dcd use a signed length variable to keep track of remaining | bytes. Continue transfer loop only if more than 0 bytes are left to | transfer. Also drop the m_ prefix, m_ is usually used for member | variables but this is a local variable. | | Signed-off-by: Stefan Agner <[email protected]>[1] https://github.com/boundarydevices/imx_usb_loader/commit/561f0377db5e1a36f5ab5e17f97d774f9f1e22ab
Signed-off-by Marco Felsch <[email protected]>--- scripts/imx/imx-usb-loader.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)scripts/imx/imx-usb-loader.c +1
-1@@ -484,12 +484,16 @@ static int transfer(int report, void *p, unsigned cnt, int *last_trans) err =libusb_bulk_transfer (usb_dev_handle, (report <3 ) ?1 :2 +EP_IN , p, cnt, last_trans,1000 ); }else { -unsigned char tmp[1028 ]; +unsigned char tmp[1028 ] = {0 }; tmp[0 ] = (unsigned char )report;
Quoted 7 lines
scripts/imx/imx-usb-loader.c static int transfer(int report, void *p, unsigned cnt, int *last_trans) · line 495
Quoted 5 lines
scripts/imx/imx-usb-loader.c +3
-0if (report <3 ) {memcpy (&tmp[1 ], p, cnt); + +if (report ==2 ) + cnt = mach_id->max_transfer;Did you verify that this can't result in an out-of-bounds read?
How should that be possible? The tmp-array is always 1028-byte and we already do limit cnt to max_transfer if cnt is larger than max_transfer.
Quoted 2 lines
I'd have expected either an adjustment to tmp's size or a description why a buffer overread can't happen in the commit message.
Sorry but can you elaborate where a buffer overrread can happen?
Regards,
Marco
Quoted 21 lines
Quoted 13 lines
scripts/imx/imx-usb-loader.c +2
-1+if (mach_id->hid_endpoint) {int trans; err = libusb_interrupt_transfer(usb_dev_handle,@@ -739,7 +743,7 @@ static int send_buf(void *buf, unsigned len)while (1 ) {int now =get_min (cnt, mach_id->max_transfer); -if (!now) +if (now <=0 )break ; err =transfer (2 , p, now, &now);Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH 4/4] imx-usb-loader: fix windows usage
Quoted 67 lines · Marco Felsch
Quoted 58 lines · Ahmad Fatoum
Quoted 57 lines · Marco Felsch
This ports commit
561f0377db5e("Transfer always specified report 2 length") from imx_usb_loader[1] to our implementation.| commit 561f0377db5e1a36f5ab5e17f97d774f9f1e22ab | Author: Stefan Agner <[email protected]> | Date: Wed Nov 2 16:51:24 2016 -0700 | | Transfer always specified report 2 length | | The Windows HIDAPI is very strict when it comes to packet lengths: | The API always expect the length specified by the device and rejects | the transfer otherwise. The report size is specified by the HID | Report Descriptor. The i.MX 6 SoC reports a report 2 size of 1024. | Currently, imx_usb reads the length from the device specific config | files and stores it in max_transfer. This change uses max_transfer | for report 2 when using the HID API. | | The boot ROM handles too long transfers just fine. Also NXP's | sb_loader transfers always complete reports. The change has been | successfully tested on Linux and Windows. | | Given that the device reports the size, it is probably also "the | right thing to do"... | | With that change, we might end up transferring up to 1023 bytes | more than actually necessary. Make sure that DoIRomDownload does | not print an error message and returns successfully even if too | many bytes have been transferred (this now typically happens at the | end of every file). | | In write_dcd use a signed length variable to keep track of remaining | bytes. Continue transfer loop only if more than 0 bytes are left to | transfer. Also drop the m_ prefix, m_ is usually used for member | variables but this is a local variable. | | Signed-off-by: Stefan Agner <[email protected]>[1] https://github.com/boundarydevices/imx_usb_loader/commit/561f0377db5e1a36f5ab5e17f97d774f9f1e22ab
Signed-off-by Marco Felsch <[email protected]>--- scripts/imx/imx-usb-loader.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)scripts/imx/imx-usb-loader.c +1
-1@@ -484,12 +484,16 @@ static int transfer(int report, void *p, unsigned cnt, int *last_trans) err =libusb_bulk_transfer (usb_dev_handle, (report <3 ) ?1 :2 +EP_IN , p, cnt, last_trans,1000 ); }else { -unsigned char tmp[1028 ]; +unsigned char tmp[1028 ] = {0 }; tmp[0 ] = (unsigned char )report;Quoted 7 lines
scripts/imx/imx-usb-loader.c static int transfer(int report, void *p, unsigned cnt, int *last_trans) · line 495
Quoted 5 lines
scripts/imx/imx-usb-loader.c +3
-0if (report <3 ) {memcpy (&tmp[1 ], p, cnt); + +if (report ==2 ) + cnt = mach_id->max_transfer;Did you verify that this can't result in an out-of-bounds read?
Quoted 2 lines
How should that be possible? The tmp-array is always 1028-byte and we already do limit cnt to
max_transferif cnt is larger thanmax_transfer.
In the case that cnt < mach_id->max_transfer, we increase cnt to mach_id->max_transfer and pass that to libusb. Are we guaranteed that libusb won't read out of bounds, i.e. buf + cnt - 1 is always inside tmp?
Quoted 5 lines
Quoted 3 lines
I'd have expected either an adjustment to tmp's size or a description why a buffer overread can't happen in the commit message.
Sorry but can you elaborate where a buffer overrread can happen?
You groked the code and wrote the patch, so I hoped you could tell me "no, buffer overflow can't happen because ...".
Cheers,
Ahmad
Quoted 26 lines
Regards,
MarcoQuoted 21 lines
Quoted 13 lines
scripts/imx/imx-usb-loader.c +2
-1+if (mach_id->hid_endpoint) {int trans; err = libusb_interrupt_transfer(usb_dev_handle,@@ -739,7 +743,7 @@ static int send_buf(void *buf, unsigned len)while (1 ) {int now =get_min (cnt, mach_id->max_transfer); -if (!now) +if (now <=0 )break ; err =transfer (2 , p, now, &now);Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH 4/4] imx-usb-loader: fix windows usage
Quoted 68 lines · Ahmad Fatoum
Quoted 67 lines · Marco Felsch
Quoted 65 lines · Ahmad Fatoum
Quoted 52 lines · Marco Felsch
This ports commit
561f0377db5e("Transfer always specified report 2 length") from imx_usb_loader[1] to our implementation.| commit 561f0377db5e1a36f5ab5e17f97d774f9f1e22ab | Author: Stefan Agner <[email protected]> | Date: Wed Nov 2 16:51:24 2016 -0700 | | Transfer always specified report 2 length | | The Windows HIDAPI is very strict when it comes to packet lengths: | The API always expect the length specified by the device and rejects | the transfer otherwise. The report size is specified by the HID | Report Descriptor. The i.MX 6 SoC reports a report 2 size of 1024. | Currently, imx_usb reads the length from the device specific config | files and stores it in max_transfer. This change uses max_transfer | for report 2 when using the HID API. | | The boot ROM handles too long transfers just fine. Also NXP's | sb_loader transfers always complete reports. The change has been | successfully tested on Linux and Windows. | | Given that the device reports the size, it is probably also "the | right thing to do"... | | With that change, we might end up transferring up to 1023 bytes | more than actually necessary. Make sure that DoIRomDownload does | not print an error message and returns successfully even if too | many bytes have been transferred (this now typically happens at the | end of every file). | | In write_dcd use a signed length variable to keep track of remaining | bytes. Continue transfer loop only if more than 0 bytes are left to | transfer. Also drop the m_ prefix, m_ is usually used for member | variables but this is a local variable. | | Signed-off-by: Stefan Agner <[email protected]>[1] https://github.com/boundarydevices/imx_usb_loader/commit/561f0377db5e1a36f5ab5e17f97d774f9f1e22ab
Signed-off-by Marco Felsch <[email protected]>--- scripts/imx/imx-usb-loader.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)scripts/imx/imx-usb-loader.c +0
-0@@ -484,12 +484,16 @@ static int transfer(int report, void *p, unsigned cnt, int *last_trans) err =libusb_bulk_transfer (usb_dev_handle, (report <3 ) ?1 :2 +EP_IN , p, cnt, last_trans,1000 ); }else {scripts/imx/imx-usb-loader.c static int transfer(int report, void *p, unsigned cnt, int *last_trans) · line 495
Quoted 10 lines
scripts/imx/imx-usb-loader.c +4
-1-unsigned char tmp[1028 ]; +unsigned char tmp[1028 ] = {0 }; tmp[0 ] = (unsigned char )report;if (report <3 ) {memcpy (&tmp[1 ], p, cnt); + +if (report ==2 ) + cnt = mach_id->max_transfer;Did you verify that this can't result in an out-of-bounds read?
Quoted 7 lines
Quoted 2 lines
How should that be possible? The tmp-array is always 1028-byte and we already do limit cnt to
max_transferif cnt is larger thanmax_transfer.In the case that cnt <
mach_id->max_transfer, we increase cnt tomach_id->max_transferand pass that to libusb. Are we guaranteed that libusb won't read out of bounds, i.e. buf + cnt - 1 is always inside tmp?
At the moment it is guaranteed that mach_id->max_transfer is always smaller than 1028. libusb always start to read at &tmp[0] so the last read may transfer useless bytes (0) but we are always within the tmp buffer. So no out-of-bound reads.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I'd have expected either an adjustment to tmp's size or a description why a buffer overread can't happen in the commit message.
Sorry but can you elaborate where a buffer overrread can happen?
You groked the code and wrote the patch, so I hoped you could tell me "no, buffer overflow can't happen because ...".
Please see above.
Regards,
Marco
Quoted 38 lines
Cheers,
AhmadQuoted 26 lines
Regards,
MarcoQuoted 21 lines
Quoted 13 lines
scripts/imx/imx-usb-loader.c +2
-1+if (mach_id->hid_endpoint) {int trans; err = libusb_interrupt_transfer(usb_dev_handle,@@ -739,7 +743,7 @@ static int send_buf(void *buf, unsigned len)while (1 ) {int now =get_min (cnt, mach_id->max_transfer); -if (!now) +if (now <=0 )break ; err =transfer (2 , p, now, &now);Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Sascha Hauer
AppliedQuoted 14 lines · Marco Felsch
Hi all,
this small series enable our imx-usb-loader and barebox-pbl implementation to get loaded via Windows.
Regards,
MarcoMarco Felsch (4):
scripts: common: fixread_file_2for windows
scripts: common: fix buffer freeing
usb: gadget:fsl_udc: lowerstate_completeconstraints
imx-usb-loader: fix windows usage
Quoted 4 lines
drivers/usb/gadget/udc/fsl_udc_pbl.c | 2 +- scripts/common.c | 5 +++-- scripts/imx/imx-usb-loader.c | 8 ++++++-- 3 files changed, 10 insertions(+), 5 deletions(-)
Applied, thanks
Sascha
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |