diff --git a/arch/arm/Makefile b/arch/arm/Makefile index 96ec588..620a3cc 100644 --- a/arch/arm/Makefile +++ b/arch/arm/Makefile @@ -32,7 +32,7 @@ # testing for a specific architecture or later rather impossible. arch-$(CONFIG_CPU_64v8) := -D__LINUX_ARM_ARCH__=8 $(call cc-option,-march=armv8-a) arch-$(CONFIG_CPU_32v7) :=-D__LINUX_ARM_ARCH__=7 $(call cc-option,-march=armv7-a,-march=armv5t -Wa$(comma)-march=armv7-a) -arch-$(CONFIG_CPU_32v6) :=-D__LINUX_ARM_ARCH__=6 $(call cc-option,-march=armv6,-march=armv5t -Wa$(comma)-march=armv6) +arch-$(CONFIG_CPU_32v6) :=-D__LINUX_ARM_ARCH__=6 $(call cc-option,-march=armv6,-march=armv5t -Wa$(comma)-march=armv6) arch-$(CONFIG_CPU_32v5) :=-D__LINUX_ARM_ARCH__=5 $(call cc-option,-march=armv5te,-march=armv4t) arch-$(CONFIG_CPU_32v4T) :=-D__LINUX_ARM_ARCH__=4 -march=armv4t diff --git a/arch/arm/boards/avnet-zedboard/flash_header.c b/arch/arm/boards/avnet-zedboard/flash_header.c index ea20524..d9eb35b 100644 --- a/arch/arm/boards/avnet-zedboard/flash_header.c +++ b/arch/arm/boards/avnet-zedboard/flash_header.c @@ -52,10 +52,10 @@ .enc_stat = 0x0, .user = 0x0, .flash_offset = 0x8c0, - .length = barebox_image_size, + .length = (unsigned int)&_barebox_image_size, .res0 = 0x0, .start_of_exec = 0x0, - .total_len = barebox_image_size, + .total_len = (unsigned int)&_barebox_image_size, .res1 = 0x1, .checksum = 0x0, .res2 = 0x0, diff --git a/arch/arm/boards/beaglebone/lowlevel.c b/arch/arm/boards/beaglebone/lowlevel.c index 100f64f..a56b4b6 100644 --- a/arch/arm/boards/beaglebone/lowlevel.c +++ b/arch/arm/boards/beaglebone/lowlevel.c @@ -41,6 +41,7 @@ .emif_tim1 = 0x0666B3C9, .emif_tim2 = 0x243631CA, .emif_tim3 = 0x0000033F, + .ocp_config = 0x00141414, .sdram_config = 0x41805332, .sdram_config2 = 0x41805332, .sdram_ref_ctrl = 0x0000081A, @@ -97,6 +98,7 @@ .emif_tim1 = 0x0AAAD4DB, .emif_tim2 = 0x266B7FDA, .emif_tim3 = 0x501F867F, + .ocp_config = 0x00141414, .zq_config = 0x50074BE4, .sdram_config = 0x61C05332, .sdram_config2 = 0x0, diff --git a/arch/arm/boards/friendlyarm-tiny210/lowlevel.c b/arch/arm/boards/friendlyarm-tiny210/lowlevel.c index 3ab8d66..fea00ef 100644 --- a/arch/arm/boards/friendlyarm-tiny210/lowlevel.c +++ b/arch/arm/boards/friendlyarm-tiny210/lowlevel.c @@ -97,7 +97,7 @@ debug_led(1, 1); if (! load_stage2((void*)(ld_var(_text) - 16), - ld_var(_barebox_image_size) + 16)) { + barebox_image_size + 16)) { debug_led(3, 1); while (1) { } /* hang */ } diff --git a/arch/arm/boards/phytec-som-am335x/ram-timings.h b/arch/arm/boards/phytec-som-am335x/ram-timings.h index 9576d26..4ea654d 100644 --- a/arch/arm/boards/phytec-som-am335x/ram-timings.h +++ b/arch/arm/boards/phytec-som-am335x/ram-timings.h @@ -45,6 +45,7 @@ .emif_tim1 = 0x0AAAD4DB, .emif_tim2 = 0x26437FDA, .emif_tim3 = 0x501F83FF, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C052B2, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, @@ -66,6 +67,7 @@ .emif_tim1 = 0x0AAAE4DB, .emif_tim2 = 0x266B7FDA, .emif_tim3 = 0x501F867F, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C05332, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, @@ -87,6 +89,7 @@ .emif_tim1 = 0x0AAAD4DB, .emif_tim2 = 0x26437FDA, .emif_tim3 = 0x501F83FF, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C052B2, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, @@ -106,6 +109,7 @@ .emif_tim1 = 0x0AAAE4DB, .emif_tim2 = 0x262F7FDA, .emif_tim3 = 0x501F82BF, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C05232, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, @@ -125,6 +129,7 @@ .emif_tim1 = 0x0AAAE4DB, .emif_tim2 = 0x266B7FDA, .emif_tim3 = 0x501F867F, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C05332, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30 @@ -144,6 +149,7 @@ .emif_tim1 = 0x0AAAE4DB, .emif_tim2 = 0x266B7FDA, .emif_tim3 = 0x501F867F, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C053B2, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30 @@ -163,6 +169,7 @@ .emif_tim1 = 0x0AAAE4DB, .emif_tim2 = 0x268F7FDA, .emif_tim3 = 0x501F88BF, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C053B2, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30 @@ -182,6 +189,7 @@ .emif_tim1 = 0x0AAAD4DB, .emif_tim2 = 0x26437FDA, .emif_tim3 = 0x501F83FF, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C052B2, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, @@ -203,6 +211,7 @@ .emif_tim1 = 0x0AAAD4DB, .emif_tim2 = 0x266B7FDA, .emif_tim3 = 0x501F867F, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C05332, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, @@ -222,6 +231,7 @@ .emif_tim1 = 0x0AAAD4DB, .emif_tim2 = 0x26437FDA, .emif_tim3 = 0x501F83FF, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C052B2, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, @@ -241,6 +251,7 @@ .emif_tim1 = 0x0AAAD4DB, .emif_tim2 = 0x268F7FDA, .emif_tim3 = 0x501F88BF, + .ocp_config = 0x003d3d3d, .sdram_config = 0x61C053B2, .zq_config = 0x50074BE4, .sdram_ref_ctrl = 0x00000C30, diff --git a/arch/arm/boards/phytec-som-rk3288/lowlevel.c b/arch/arm/boards/phytec-som-rk3288/lowlevel.c index 7804a55..7649ef8 100644 --- a/arch/arm/boards/phytec-som-rk3288/lowlevel.c +++ b/arch/arm/boards/phytec-som-rk3288/lowlevel.c @@ -30,14 +30,13 @@ if (IS_ENABLED(CONFIG_DEBUG_LL)) { struct rk3288_grf * const grf = (void *)RK3288_GRF_BASE; - rk_clrsetreg(&grf->gpio4c_iomux, - GPIO4C1_MASK << GPIO4C1_SHIFT | - GPIO4C0_MASK << GPIO4C0_SHIFT, - GPIO4C1_UART0BT_SOUT << GPIO4C1_SHIFT | - GPIO4C0_UART0BT_SIN << GPIO4C0_SHIFT); + rk_clrsetreg(&grf->gpio7ch_iomux, + GPIO7C7_MASK << GPIO7C7_SHIFT | + GPIO7C6_MASK << GPIO7C6_SHIFT, + GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT | + GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT); INIT_LL(); } - fdt = __dtb_rk3288_phycore_som_start - get_runtime_offset(); barebox_arm_entry(0x0, SZ_1G, fdt); diff --git a/arch/arm/configs/rk3288_defconfig b/arch/arm/configs/rk3288_defconfig index f54f4cc..15e6c15 100644 --- a/arch/arm/configs/rk3288_defconfig +++ b/arch/arm/configs/rk3288_defconfig @@ -26,7 +26,7 @@ CONFIG_RESET_SOURCE=y CONFIG_DEBUG_INFO=y CONFIG_DEBUG_LL=y -CONFIG_DEBUG_ROCKCHIP_UART_PORT=0 +CONFIG_DEBUG_ROCKCHIP_UART_PORT=2 CONFIG_CMD_DMESG=y CONFIG_LONGHELP=y CONFIG_CMD_IOMEM=y diff --git a/arch/arm/cpu/common.c b/arch/arm/cpu/common.c index 46ce942..dcd8f0b 100644 --- a/arch/arm/cpu/common.c +++ b/arch/arm/cpu/common.c @@ -78,3 +78,6 @@ return __cpu_architecture; } #endif + +char __image_start[0] __attribute__((section(".__image_start"))); +char __image_end[0] __attribute__((section(".__image_end"))); \ No newline at end of file diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c index a62b0d5..171e6ad 100644 --- a/arch/arm/cpu/start.c +++ b/arch/arm/cpu/start.c @@ -116,7 +116,7 @@ { unsigned long mem; - mem = arm_mem_barebox_image(membase, endmem, barebox_image_size); + mem = arm_mem_barebox_image(membase, endmem, arm_barebox_size); mem -= ALIGN(size, 64); return mem; diff --git a/arch/arm/dts/rk3288-phycore-som.dts b/arch/arm/dts/rk3288-phycore-som.dts index 5410bd1..dd74bcf 100644 --- a/arch/arm/dts/rk3288-phycore-som.dts +++ b/arch/arm/dts/rk3288-phycore-som.dts @@ -44,7 +44,7 @@ }; chosen { - stdout-path = &uart0; + stdout-path = &uart2; environment-emmc { compatible = "barebox,environment"; diff --git a/arch/arm/include/asm/barebox-arm.h b/arch/arm/include/asm/barebox-arm.h index e8dfd02..3aea2e0 100644 --- a/arch/arm/include/asm/barebox-arm.h +++ b/arch/arm/include/asm/barebox-arm.h @@ -179,4 +179,6 @@ */ #define MAX_BSS_SIZE SZ_1M +#define barebox_image_size (__image_end - __image_start) + #endif /* _BAREBOX_ARM_H_ */ diff --git a/arch/arm/lib32/barebox.lds.S b/arch/arm/lib32/barebox.lds.S index 6dc8bd2..5fd39dc 100644 --- a/arch/arm/lib32/barebox.lds.S +++ b/arch/arm/lib32/barebox.lds.S @@ -30,6 +30,7 @@ #else . = TEXT_BASE; #endif + .image_start : { *(.__image_start) } #ifndef CONFIG_PBL_IMAGE PRE_IMAGE @@ -116,6 +117,7 @@ } _edata = .; + .image_end : { *(.__image_end) } . = ALIGN(4); __bss_start = .; diff --git a/arch/arm/lib32/runtime-offset.S b/arch/arm/lib32/runtime-offset.S index f10c4c8..7375cb9 100644 --- a/arch/arm/lib32/runtime-offset.S +++ b/arch/arm/lib32/runtime-offset.S @@ -39,7 +39,6 @@ ld_var_entry __rel_dyn_end ld_var_entry __dynsym_start ld_var_entry __dynsym_end -ld_var_entry _barebox_image_size ld_var_entry __bss_start ld_var_entry __bss_stop #ifdef __PBL__ diff --git a/arch/arm/mach-omap/Kconfig b/arch/arm/mach-omap/Kconfig index 93fa35a..9c41741 100644 --- a/arch/arm/mach-omap/Kconfig +++ b/arch/arm/mach-omap/Kconfig @@ -149,6 +149,7 @@ select XYMODEM select FS_RAMFS depends on ARCH_AM33XX && SHELL_NONE + depends on !CONSOLE_NONE help Say Y here if you want to load the 2nd stage barebox.bin with xmodem after booting from serial line. diff --git a/arch/arm/mach-omap/am33xx_generic.c b/arch/arm/mach-omap/am33xx_generic.c index 5eead5c..5137462 100644 --- a/arch/arm/mach-omap/am33xx_generic.c +++ b/arch/arm/mach-omap/am33xx_generic.c @@ -341,6 +341,9 @@ writel(regs->emif_tim3, AM33XX_EMIF4_0_REG(SDRAM_TIM_3)); writel(regs->emif_tim3, AM33XX_EMIF4_0_REG(SDRAM_TIM_3_SHADOW)); + if (regs->ocp_config) + writel(regs->ocp_config, AM33XX_EMIF4_0_REG(OCP_CONFIG)); + if (regs->zq_config) { /* * A value of 0x2800 for the REF CTRL will give us diff --git a/arch/arm/mach-omap/include/mach/am33xx-silicon.h b/arch/arm/mach-omap/include/mach/am33xx-silicon.h index 10595d5..0729369 100644 --- a/arch/arm/mach-omap/include/mach/am33xx-silicon.h +++ b/arch/arm/mach-omap/include/mach/am33xx-silicon.h @@ -114,6 +114,7 @@ #define EMIF4_SDRAM_TIM_3_SHADOW 0x2C #define EMIF0_SDRAM_MGMT_CTRL 0x38 #define EMIF0_SDRAM_MGMT_CTRL_SHD 0x3C +#define EMIF4_OCP_CONFIG 0x54 #define EMIF4_ZQ_CONFIG 0xC8 #define EMIF4_DDR_PHY_CTRL_1 0xE4 #define EMIF4_DDR_PHY_CTRL_1_SHADOW 0xE8 @@ -217,6 +218,7 @@ u32 emif_tim1; u32 emif_tim2; u32 emif_tim3; + u32 ocp_config; u32 sdram_config; u32 sdram_config2; u32 zq_config; diff --git a/drivers/mtd/nand/nand_s3c24xx.c b/drivers/mtd/nand/nand_s3c24xx.c index 83d4517..df22735 100644 --- a/drivers/mtd/nand/nand_s3c24xx.c +++ b/drivers/mtd/nand/nand_s3c24xx.c @@ -614,7 +614,7 @@ void __nand_boot_init nand_boot(void) { void *dest = _text; - int size = ld_var(_barebox_image_size); + int size = barebox_image_size; int page = 0; s3c24x0_nand_load_image(dest, size, page); diff --git a/drivers/serial/serial_ns16550.c b/drivers/serial/serial_ns16550.c index c6548e3..439b7d5 100644 --- a/drivers/serial/serial_ns16550.c +++ b/drivers/serial/serial_ns16550.c @@ -287,12 +287,31 @@ static void ns16550_probe_dt(struct device_d *dev, struct ns16550_priv *priv) { struct device_node *np = dev->device_node; + u32 width; if (!IS_ENABLED(CONFIG_OFDEVICE)) return; of_property_read_u32(np, "clock-frequency", &priv->plat.clock); of_property_read_u32(np, "reg-shift", &priv->plat.shift); + if (!of_property_read_u32(np, "reg-io-width", &width)) + switch (width) { + case 1: + priv->read_reg = ns16550_read_reg_mmio_8; + priv->write_reg = ns16550_write_reg_mmio_8; + break; + case 2: + priv->read_reg = ns16550_read_reg_mmio_16; + priv->write_reg = ns16550_write_reg_mmio_16; + break; + case 4: + priv->read_reg = ns16550_read_reg_mmio_32; + priv->write_reg = ns16550_write_reg_mmio_32; + break; + default: + dev_err(dev, "unsupported reg-io-width (%d)\n", + width); + } } static struct ns16550_drvdata ns16450_drvdata = { diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h index 984f8b6..0eb18f6 100644 --- a/include/asm-generic/sections.h +++ b/include/asm-generic/sections.h @@ -6,12 +6,13 @@ extern char _sdata[], _edata[]; extern char __bare_init_start[], __bare_init_end[]; extern char _end[]; +extern char __image_start[]; extern char __image_end[]; extern void *_barebox_image_size; extern void *_barebox_bare_init_size; extern void *_barebox_pbl_size; -#define barebox_image_size (unsigned int)&_barebox_image_size +#define barebox_image_size (__image_end - __image_start) #define barebox_bare_init_size (unsigned int)&_barebox_bare_init_size #define barebox_pbl_size (unsigned int)&_barebox_pbl_size diff --git a/scripts/.gitignore b/scripts/.gitignore index 533bffd..2a9ae6b 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore @@ -25,3 +25,4 @@ mxsimage mxsboot mxs-usb-loader +omap4_usbboot diff --git a/scripts/Makefile b/scripts/Makefile index a5c16b2..7f2527d 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -28,9 +28,11 @@ HOSTCFLAGS_omap3-usb-loader.o = `pkg-config --cflags libusb-1.0` HOSTLOADLIBES_omap3-usb-loader = `pkg-config --libs libusb-1.0` hostprogs-$(CONFIG_OMAP3_USB_LOADER) += omap3-usb-loader +HOSTCFLAGS_omap4_usbboot.o = `pkg-config --cflags libusb-1.0` +HOSTLOADLIBES_omap4_usbboot = -lpthread `pkg-config --libs libusb-1.0` +hostprogs-$(CONFIG_OMAP4_USBBOOT) += omap4_usbboot subdir-y += mod -subdir-$(CONFIG_OMAP4_USBBOOT) += omap4_usbboot subdir-$(CONFIG_ARCH_IMX) += imx subdir-$(CONFIG_X86) += setupmbr subdir-$(CONFIG_DTC) += dtc diff --git a/scripts/omap4_usbboot.c b/scripts/omap4_usbboot.c new file mode 100644 index 0000000..329668d --- /dev/null +++ b/scripts/omap4_usbboot.c @@ -0,0 +1,455 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Inspired by: https://github.com/simu/usbboot-omap4.git + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define USBBOOT_FS_MAGIC 0x5562464D +#define USBBOOT_FS_CMD_OPEN 0x46530000 +#define USBBOOT_FS_CMD_CLOSE 0x46530001 +#define USBBOOT_FS_CMD_READ 0x46530002 +#define USBBOOT_FS_CMD_END 0x4653FFFF +#define MAX_OPEN_FILES 128 + +#define RESET 0 +#define BRIGHT 1 +#define WHITE 8 +#define RED 1 +#define BLACK 0 +#define TFORMAT "%c[%d;%dm" +#define HFORMAT "%c[%dm" +#define TARGET_FORMAT 0x1B, BRIGHT, RED+30 +#define HOST_FORMAT 0x1B, RESET +#define host_print(fmt, arg...) printf(HFORMAT fmt TFORMAT, \ + HOST_FORMAT, ##arg, TARGET_FORMAT) + +int usb_write(void *h, void const *data, int len) +{ + int actual; + return libusb_bulk_transfer(h, 0x01, (void *)data, len, &actual, 5000) ? + 0 : actual; +} + +int usb_read(void *h, void *data, int len) +{ + int actual; + return libusb_bulk_transfer(h, 0x81, data, len, &actual, 5000) ? + 0 : actual; +} + +void panic(struct termios *t_restore) +{ + tcsetattr(STDIN_FILENO, TCSANOW, t_restore); + printf(HFORMAT, HOST_FORMAT); + exit(1); +} + +struct thread_vars { + struct libusb_device_handle *usb; + pthread_mutex_t usb_mutex; + struct termios t_restore; +}; + +void *listenerTask(void *argument) +{ + struct thread_vars *vars = argument; + int c; + for (;;) { + c = getchar(); + if (c == EOF) + return NULL; + pthread_mutex_lock(&vars->usb_mutex); + if (usb_write(vars->usb, &c, 4) != 4) { + host_print("could not send '%c' to target\n", c); + panic(&vars->t_restore); + } + pthread_mutex_unlock(&vars->usb_mutex); + } + return NULL; +} + +int read_asic_id(struct libusb_device_handle *usb) +{ +#define LINEWIDTH 16 + const uint32_t msg_getid = 0xF0030003; + int i, j, k, ret; + uint8_t id[81]; + char line[LINEWIDTH*3+5]; + + printf("reading ASIC ID\n"); + memset(id , 0xee, sizeof(id)); + if (usb_write(usb, &msg_getid, sizeof(msg_getid)) != + sizeof(msg_getid) + ) { + printf("Could not send msg_getid request\n"); + return -1; + } + if (usb_read(usb, id, sizeof(id)) != sizeof(id)) { + printf("Could not read msg_getid answer\n"); + return -1; + } + for (i = 0; i < sizeof(id); i += LINEWIDTH) { + sprintf(line, "%02X: ", i); + for (j = 0; j < LINEWIDTH && j < sizeof(id)-i; j++) + sprintf(line+4+j*3, "%02X ", id[i+j]); + line[4+j*3] = 0; + puts(line); + } + ret = 0; + for (i = 1, j = 0; i < sizeof(id) && j < id[0]; i += 2+id[i+1], j++) { + if (i+2+id[i+1] > sizeof(id)) { + printf("Truncated subblock\n"); + ret++; + continue; + } + switch (id[i]) { + case 0x01: /* ID subblock */ + if (id[i+1] != 0x05) { + printf("Unexpected ID subblock size\n"); + ret++; + continue; + } + if (id[i+2] != 0x01) + printf("Unexpected fixed value\n"); + k = (id[i+3]<<8) | id[i+4]; + switch (k) { + case 0x4440: + printf("OMAP 4460 Device\n"); + break; + default: + printf("Unknown Device\n"); + break; + } + switch (id[i+5]) { + case 0x07: + printf("CH enabled (read from eFuse)\n"); + break; + case 0x17: + printf("CH disabled (read from eFuse)\n"); + break; + default: + printf("Unknown CH setting\n"); + break; + } + printf("Rom version: %hhu\n", id[i+6]); + break; + case 0x15: /* Checksum subblock */ + if (id[i+1] != 0x09) { + printf("Unexpected Checksum subblock size\n"); + ret++; + continue; + } + if (id[i+2] != 0x01) + printf("Unexpected fixed value\n"); + k = (id[i+3]<<24) | (id[i+4]<<16) | + (id[i+5]<<8) | id[i+6]; + printf("Rom CRC: 0x%08X\n", k); + k = (id[i+7]<<24) | (id[i+8]<<16) | + (id[i+9]<<8) | id[i+10]; + switch (k) { + case 0: + printf("A GP device\n"); + break; + default: + printf("Unknown device\n"); + break; + } + break; + } + } + if (i != sizeof(id) || j != id[0]) { + printf("Unexpected ASIC ID structure size.\n"); + ret++; + } + return ret; +} + +struct file_data { + size_t size; + void *data; +}; + +int process_file(struct libusb_device_handle *usb, const char *rootfs, + struct file_data *fd_vector, struct termios *t_restore) +{ + uint32_t i, j, pos, size; + struct stat s; + int fd, ret; + char fname[256]; + + if (usb_read(usb, &i, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + ret = 0; + switch (i) { + case USBBOOT_FS_CMD_OPEN: + for (j = 0; rootfs[j]; j++) + fname[j] = rootfs[j]; + for (;; j++) { + if (usb_read(usb, &i, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + if (i == USBBOOT_FS_CMD_END) { + fname[j] = 0; + break; + } else if (i > 0xFF) { + host_print("Error in filename\n"); + ret++; + fname[j] = 0; + break; + } else + fname[j] = i; + } + for (i = 0; i < MAX_OPEN_FILES && fd_vector[i].data; i++) + ; + if (i >= MAX_OPEN_FILES) { + host_print("MAX_OPEN_FILES exceeded\n"); + ret++; + goto open_error_1; + } + fd = open(fname, O_RDONLY); + if (fd < 0) { + host_print("cannot open '%s'\n", fname); + ret++; + goto open_error_1; + } + if (fstat(fd, &s)) { + host_print("cannot stat '%s'\n", fname); + ret++; + goto open_error_2; + } + fd_vector[i].data = mmap(NULL, s.st_size, PROT_READ, + MAP_PRIVATE, fd, 0); + if (fd_vector[i].data == MAP_FAILED) { + host_print("cannot mmap '%s'\n", fname); + ret++; + goto open_error_2; + } + close(fd); + fd_vector[i].size = size = s.st_size; + fd = i; + goto open_ok; + +open_error_2: + close(fd); +open_error_1: + fd_vector[i].size = size = 0; + fd_vector[i].data = NULL; + fd = -1; +open_ok: + if (usb_write(usb, &fd, 4) != 4 || + usb_write(usb, &size, 4) != 4 + ) { + host_print("could not send file size to target\n"); + panic(t_restore); + } + break; + case USBBOOT_FS_CMD_CLOSE: + if (usb_read(usb, &i, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + if (i >= MAX_OPEN_FILES || !fd_vector[i].data) { + host_print("invalid close index\n"); + ret++; + break; + } + if (usb_read(usb, &j, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + if (j != USBBOOT_FS_CMD_END) { + host_print("invalid close\n"); + ret++; + break; + } + munmap(fd_vector[i].data, fd_vector[i].size); + fd_vector[i].data = NULL; + break; + case USBBOOT_FS_CMD_READ: + if (usb_read(usb, &i, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + if (i >= MAX_OPEN_FILES || !fd_vector[i].data) { + host_print("invalid read index\n"); + ret++; + break; + } + if (usb_read(usb, &pos, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + if (pos >= fd_vector[i].size) { + host_print("invalid read pos\n"); + ret++; + break; + } + if (usb_read(usb, &size, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + if (pos+size > fd_vector[i].size) { + host_print("invalid read size\n"); + ret++; + break; + } + if (usb_read(usb, &j, 4) != 4) { + host_print("USB error\n"); + panic(t_restore); + } + if (j != USBBOOT_FS_CMD_END) { + host_print("invalid read\n"); + ret++; + break; + } + if (usb_write(usb, fd_vector[i].data+pos, size) != size) { + host_print("could not send file to target\n"); + panic(t_restore); + } + break; + case USBBOOT_FS_CMD_END: + default: + host_print("Unknown filesystem command\n"); + ret++; + break; + } + return ret; +} + +int usb_boot(struct libusb_device_handle *usb, + void *data, unsigned sz, const char *rootfs) +{ + const uint32_t msg_boot = 0xF0030002; + uint32_t msg_size = sz; + int i; + pthread_t thread; + struct thread_vars vars; + struct termios tn; + struct file_data fd_vector[MAX_OPEN_FILES]; + + if (read_asic_id(usb)) + return -1; + + printf("sending xload to target...\n"); + usleep(1000); + usb_write(usb, &msg_boot, sizeof(msg_boot)); + usleep(1000); + usb_write(usb, &msg_size, sizeof(msg_size)); + usleep(1000); + usb_write(usb, data, sz); + usleep(100000); + munmap(data, msg_size); + for (i = 0; i < MAX_OPEN_FILES; i++) + fd_vector[i].data = NULL; + + vars.usb = usb; + pthread_mutex_init(&vars.usb_mutex, NULL); + tcgetattr(STDIN_FILENO, &vars.t_restore); + tn = vars.t_restore; + tn.c_lflag &= ~(ICANON | ECHO); + printf(TFORMAT, TARGET_FORMAT); + tcsetattr(STDIN_FILENO, TCSANOW, &tn); + if (pthread_create(&thread, NULL, listenerTask, &vars)) + host_print("listenerTask failed\n"); + for (;;) { + usleep(100); + if (usb_read(usb, &i, 4) != 4) + break; + if (i == USBBOOT_FS_MAGIC) { + usleep(100); + pthread_mutex_lock(&vars.usb_mutex); + process_file(usb, rootfs, fd_vector, &vars.t_restore); + pthread_mutex_unlock(&vars.usb_mutex); + continue; + } + printf("%c", i); + fflush(stdout); + } + pthread_mutex_destroy(&vars.usb_mutex); + tcsetattr(STDIN_FILENO, TCSANOW, &vars.t_restore); + printf(HFORMAT, HOST_FORMAT); + return 0; +} + +int main(int argc, char **argv) +{ + void *data; + unsigned sz; + struct stat s; + int fd; + int ret; + struct libusb_context *ctx = NULL; + struct libusb_device_handle *usb = NULL; + + if (argc != 3) { + printf("usage: %s \n", argv[0]); + return 0; + } + argv++; + fd = open(argv[0], O_RDONLY); + if (fd < 0 || fstat(fd, &s)) { + printf("cannot open '%s'\n", argv[0]); + return -1; + } + data = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (data == MAP_FAILED) { + printf("cannot mmap '%s'\n", argv[0]); + return -1; + } + sz = s.st_size; + close(fd); + argv++; + if (libusb_init(&ctx)) { + printf("cannot initialize libusb\n"); + return -1; + } + printf(HFORMAT, HOST_FORMAT); + printf("waiting for OMAP44xx device...\n"); + while (1) { + if (!usb) + usb = libusb_open_device_with_vid_pid( + ctx, 0x0451, 0xD010); + if (!usb) + usb = libusb_open_device_with_vid_pid( + ctx, 0x0451, 0xD00F); + if (usb) { + libusb_detach_kernel_driver(usb, 0); + ret = libusb_set_configuration(usb, 1); + if (ret) + break; + ret = libusb_claim_interface(usb, 0); + if (ret) + break; + ret = usb_boot(usb, data, sz, argv[0]); + break; + } + usleep(250000); + } + libusb_release_interface(usb, 0); + libusb_close(usb); + libusb_exit(ctx); + return ret; +} diff --git a/scripts/omap4_usbboot/.gitignore b/scripts/omap4_usbboot/.gitignore deleted file mode 100644 index 1975a21..0000000 --- a/scripts/omap4_usbboot/.gitignore +++ /dev/null @@ -1 +0,0 @@ -omap4_usbboot diff --git a/scripts/omap4_usbboot/Makefile b/scripts/omap4_usbboot/Makefile deleted file mode 100644 index af6444b..0000000 --- a/scripts/omap4_usbboot/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -HOSTLOADLIBES_omap4_usbboot = -lpthread -omap4_usbboot-objs := usb_linux.o omap4_usbboot.o -hostprogs-$(CONFIG_OMAP4_USBBOOT) += omap4_usbboot - -always := $(hostprogs-y) diff --git a/scripts/omap4_usbboot/omap4_usbboot.c b/scripts/omap4_usbboot/omap4_usbboot.c deleted file mode 100644 index 0e5abcb..0000000 --- a/scripts/omap4_usbboot/omap4_usbboot.c +++ /dev/null @@ -1,431 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "usb.h" - -#define USBBOOT_FS_MAGIC 0x5562464D -#define USBBOOT_FS_CMD_OPEN 0x46530000 -#define USBBOOT_FS_CMD_CLOSE 0x46530001 -#define USBBOOT_FS_CMD_READ 0x46530002 -#define USBBOOT_FS_CMD_END 0x4653FFFF -#define MAX_OPEN_FILES 128 - -#define RESET 0 -#define BRIGHT 1 -#define WHITE 8 -#define RED 1 -#define BLACK 0 -#define TFORMAT "%c[%d;%dm" -#define HFORMAT "%c[%dm" -#define TARGET_FORMAT 0x1B, BRIGHT, RED+30 -#define HOST_FORMAT 0x1B, RESET -#define host_print(fmt, arg...) printf(HFORMAT fmt TFORMAT, \ - HOST_FORMAT, ##arg, TARGET_FORMAT) - -void panic(struct termios *t_restore) -{ - tcsetattr(STDIN_FILENO, TCSANOW, t_restore); - printf(HFORMAT, HOST_FORMAT); - exit(1); -} - -struct thread_vars { - struct usb_handle *usb; - pthread_mutex_t usb_mutex; - struct termios t_restore; -}; - -void *listenerTask(void *argument) -{ - struct thread_vars *vars = argument; - int c; - for (;;) { - c = getchar(); - if (c == EOF) - return NULL; - pthread_mutex_lock(&vars->usb_mutex); - if (usb_write(vars->usb, &c, 4) != 4) { - host_print("could not send '%c' to target\n", c); - panic(&vars->t_restore); - } - pthread_mutex_unlock(&vars->usb_mutex); - } - return NULL; -} - -int read_asic_id(struct usb_handle *usb) -{ -#define LINEWIDTH 16 - const uint32_t msg_getid = 0xF0030003; - int i, j, k, ret; - uint8_t id[81]; - char line[LINEWIDTH*3+5]; - - printf("reading ASIC ID\n"); - memset(id , 0xee, sizeof(id)); - if (usb_write(usb, &msg_getid, sizeof(msg_getid)) != - sizeof(msg_getid) - ) { - printf("Could not send msg_getid request\n"); - return -1; - } - if (usb_read(usb, id, sizeof(id)) != sizeof(id)) { - printf("Could not read msg_getid answer\n"); - return -1; - } - for (i = 0; i < sizeof(id); i += LINEWIDTH) { - sprintf(line, "%02X: ", i); - for (j = 0; j < LINEWIDTH && j < sizeof(id)-i; j++) - sprintf(line+4+j*3, "%02X ", id[i+j]); - line[4+j*3] = 0; - puts(line); - } - ret = 0; - for (i = 1, j = 0; i < sizeof(id) && j < id[0]; i += 2+id[i+1], j++) { - if (i+2+id[i+1] > sizeof(id)) { - printf("Truncated subblock\n"); - ret++; - continue; - } - switch (id[i]) { - case 0x01: /* ID subblock */ - if (id[i+1] != 0x05) { - printf("Unexpected ID subblock size\n"); - ret++; - continue; - } - if (id[i+2] != 0x01) - printf("Unexpected fixed value\n"); - k = (id[i+3]<<8) | id[i+4]; - switch (k) { - case 0x4440: - printf("OMAP 4460 Device\n"); - break; - default: - printf("Unknown Device\n"); - break; - } - switch (id[i+5]) { - case 0x07: - printf("CH enabled (read from eFuse)\n"); - break; - case 0x17: - printf("CH disabled (read from eFuse)\n"); - break; - default: - printf("Unknown CH setting\n"); - break; - } - printf("Rom version: %hhu\n", id[i+6]); - break; - case 0x15: /* Checksum subblock */ - if (id[i+1] != 0x09) { - printf("Unexpected Checksum subblock size\n"); - ret++; - continue; - } - if (id[i+2] != 0x01) - printf("Unexpected fixed value\n"); - k = (id[i+3]<<24) | (id[i+4]<<16) | - (id[i+5]<<8) | id[i+6]; - printf("Rom CRC: 0x%08X\n", k); - k = (id[i+7]<<24) | (id[i+8]<<16) | - (id[i+9]<<8) | id[i+10]; - switch (k) { - case 0: - printf("A GP device\n"); - break; - default: - printf("Unknown device\n"); - break; - } - break; - } - } - if (i != sizeof(id) || j != id[0]) { - printf("Unexpected ASIC ID structure size.\n"); - ret++; - } - return ret; -} - -struct file_data { - size_t size; - void *data; -}; - -int process_file(struct usb_handle *usb, const char *rootfs, - struct file_data *fd_vector, struct termios *t_restore) -{ - uint32_t i, j, pos, size; - struct stat s; - int fd, ret; - char fname[256]; - - if (usb_read(usb, &i, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - ret = 0; - switch (i) { - case USBBOOT_FS_CMD_OPEN: - for (j = 0; rootfs[j]; j++) - fname[j] = rootfs[j]; - for (;; j++) { - if (usb_read(usb, &i, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - if (i == USBBOOT_FS_CMD_END) { - fname[j] = 0; - break; - } else if (i > 0xFF) { - host_print("Error in filename\n"); - ret++; - fname[j] = 0; - break; - } else - fname[j] = i; - } - for (i = 0; i < MAX_OPEN_FILES && fd_vector[i].data; i++) - ; - if (i >= MAX_OPEN_FILES) { - host_print("MAX_OPEN_FILES exceeded\n"); - ret++; - goto open_error_1; - } - fd = open(fname, O_RDONLY); - if (fd < 0) { - host_print("cannot open '%s'\n", fname); - ret++; - goto open_error_1; - } - if (fstat(fd, &s)) { - host_print("cannot stat '%s'\n", fname); - ret++; - goto open_error_2; - } - fd_vector[i].data = mmap(NULL, s.st_size, PROT_READ, - MAP_PRIVATE, fd, 0); - if (fd_vector[i].data == MAP_FAILED) { - host_print("cannot mmap '%s'\n", fname); - ret++; - goto open_error_2; - } - close(fd); - fd_vector[i].size = size = s.st_size; - fd = i; - goto open_ok; - -open_error_2: - close(fd); -open_error_1: - fd_vector[i].size = size = 0; - fd_vector[i].data = NULL; - fd = -1; -open_ok: - if (usb_write(usb, &fd, 4) != 4 || - usb_write(usb, &size, 4) != 4 - ) { - host_print("could not send file size to target\n"); - panic(t_restore); - } - break; - case USBBOOT_FS_CMD_CLOSE: - if (usb_read(usb, &i, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - if (i >= MAX_OPEN_FILES || !fd_vector[i].data) { - host_print("invalid close index\n"); - ret++; - break; - } - if (usb_read(usb, &j, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - if (j != USBBOOT_FS_CMD_END) { - host_print("invalid close\n"); - ret++; - break; - } - munmap(fd_vector[i].data, fd_vector[i].size); - fd_vector[i].data = NULL; - break; - case USBBOOT_FS_CMD_READ: - if (usb_read(usb, &i, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - if (i >= MAX_OPEN_FILES || !fd_vector[i].data) { - host_print("invalid read index\n"); - ret++; - break; - } - if (usb_read(usb, &pos, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - if (pos >= fd_vector[i].size) { - host_print("invalid read pos\n"); - ret++; - break; - } - if (usb_read(usb, &size, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - if (pos+size > fd_vector[i].size) { - host_print("invalid read size\n"); - ret++; - break; - } - if (usb_read(usb, &j, 4) != 4) { - host_print("USB error\n"); - panic(t_restore); - } - if (j != USBBOOT_FS_CMD_END) { - host_print("invalid read\n"); - ret++; - break; - } - if (usb_write(usb, fd_vector[i].data+pos, size) != size) { - host_print("could not send file to target\n"); - panic(t_restore); - } - break; - case USBBOOT_FS_CMD_END: - default: - host_print("Unknown filesystem command\n"); - ret++; - break; - } - return ret; -} - -int usb_boot( - struct usb_handle *usb, void *data, unsigned sz, const char *rootfs) -{ - const uint32_t msg_boot = 0xF0030002; - uint32_t msg_size = sz; - int i; - pthread_t thread; - struct thread_vars vars; - struct termios tn; - struct file_data fd_vector[MAX_OPEN_FILES]; - - if (read_asic_id(usb)) - return -1; - - printf("sending xload to target...\n"); - usleep(1000); - usb_write(usb, &msg_boot, sizeof(msg_boot)); - usleep(1000); - usb_write(usb, &msg_size, sizeof(msg_size)); - usleep(1000); - usb_write(usb, data, sz); - usleep(100000); - munmap(data, msg_size); - for (i = 0; i < MAX_OPEN_FILES; i++) - fd_vector[i].data = NULL; - - vars.usb = usb; - pthread_mutex_init(&vars.usb_mutex, NULL); - tcgetattr(STDIN_FILENO, &vars.t_restore); - tn = vars.t_restore; - tn.c_lflag &= ~(ICANON | ECHO); - printf(TFORMAT, TARGET_FORMAT); - tcsetattr(STDIN_FILENO, TCSANOW, &tn); - if (pthread_create(&thread, NULL, listenerTask, &vars)) - host_print("listenerTask failed\n"); - for (;;) { - usleep(100); - if (usb_read(usb, &i, 4) != 4) - break; - if (i == USBBOOT_FS_MAGIC) { - usleep(100); - pthread_mutex_lock(&vars.usb_mutex); - process_file(usb, rootfs, fd_vector, &vars.t_restore); - pthread_mutex_unlock(&vars.usb_mutex); - continue; - } - printf("%c", i); - fflush(stdout); - } - usb_close(usb); - pthread_mutex_destroy(&vars.usb_mutex); - tcsetattr(STDIN_FILENO, TCSANOW, &vars.t_restore); - printf(HFORMAT, HOST_FORMAT); - return 0; -} - -int match_omap4_bootloader(struct usb_ifc_info *ifc) -{ - if (ifc->dev_vendor != 0x0451) - return -1; - if ((ifc->dev_product != 0xD010) && (ifc->dev_product != 0xD00F)) - return -1; - return 0; -} - -int main(int argc, char **argv) -{ - void *data; - unsigned sz; - struct stat s; - int fd; - struct usb_handle *usb; - int once; - - if (argc != 3) { - printf("usage: %s \n", argv[0]); - return 0; - } - argv++; - fd = open(argv[0], O_RDONLY); - if (fd < 0 || fstat(fd, &s)) { - printf("cannot open '%s'\n", argv[0]); - return -1; - } - data = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (data == MAP_FAILED) { - printf("cannot mmap '%s'\n", argv[0]); - return -1; - } - sz = s.st_size; - close(fd); - argv++; - printf(HFORMAT, HOST_FORMAT); - for (once = 1;;) { - usb = usb_open(match_omap4_bootloader); - if (usb) - return usb_boot(usb, data, sz, argv[0]); - if (once) { - once = 0; - printf("waiting for OMAP44xx device...\n"); - } - usleep(250000); - } - return -1; -} diff --git a/scripts/omap4_usbboot/usb.h b/scripts/omap4_usbboot/usb.h deleted file mode 100644 index d50aa6a..0000000 --- a/scripts/omap4_usbboot/usb.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef _USB_H_ -#define _USB_H_ - -struct usb_ifc_info { - /* from device descriptor */ - unsigned short dev_vendor; - unsigned short dev_product; - - unsigned char dev_class; - unsigned char dev_subclass; - unsigned char dev_protocol; - - unsigned char ifc_class; - unsigned char ifc_subclass; - unsigned char ifc_protocol; - - unsigned char has_bulk_in; - unsigned char has_bulk_out; - - unsigned char writable; - - char serial_number[256]; -}; - -typedef int (*ifc_match_func)(struct usb_ifc_info *ifc); - -struct usb_handle *usb_open(ifc_match_func callback); -int usb_close(struct usb_handle *h); -int usb_read(struct usb_handle *h, void *_data, int len); -int usb_write(struct usb_handle *h, const void *_data, int len); - - -#endif diff --git a/scripts/omap4_usbboot/usb_linux.c b/scripts/omap4_usbboot/usb_linux.c deleted file mode 100644 index 9a6e0b8..0000000 --- a/scripts/omap4_usbboot/usb_linux.c +++ /dev/null @@ -1,397 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "usb.h" - -#define MAX_RETRIES 2 - -#ifdef TRACE_USB -#define DBG1(x...) fprintf(stderr, x) -#define DBG(x...) fprintf(stderr, x) -#else -#define DBG(x...) -#define DBG1(x...) -#endif - -struct usb_handle { - char fname[64]; - int desc; - unsigned char ep_in; - unsigned char ep_out; -}; - -static inline int badname(const char *name) -{ - while (*name) { - if (!isdigit(*name++)) - return 1; - } - return 0; -} - -static int check(void *_desc, int len, unsigned type, int size) -{ - unsigned char *desc = _desc; - - if (len < size) - return -1; - if (desc[0] < size) - return -1; - if (desc[0] > len) - return -1; - if (desc[1] != type) - return -1; - - return 0; -} - -static int filter_usb_device(int fd, char *ptr, int len, int writable, - ifc_match_func callback, int *ept_in_id, int *ept_out_id, int *ifc_id) -{ - struct usb_device_descriptor *dev; - struct usb_config_descriptor *cfg; - struct usb_interface_descriptor *ifc; - struct usb_endpoint_descriptor *ept; - struct usb_ifc_info info; - - int in, out; - unsigned i; - unsigned e; - - if (check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE)) - return -1; - dev = (void *) ptr; - len -= dev->bLength; - ptr += dev->bLength; - - if (check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE)) - return -1; - cfg = (void *) ptr; - len -= cfg->bLength; - ptr += cfg->bLength; - - info.dev_vendor = dev->idVendor; - info.dev_product = dev->idProduct; - info.dev_class = dev->bDeviceClass; - info.dev_subclass = dev->bDeviceSubClass; - info.dev_protocol = dev->bDeviceProtocol; - info.writable = writable; - - /* read device serial number (if there is one) */ - info.serial_number[0] = 0; - if (dev->iSerialNumber) { - struct usbdevfs_ctrltransfer ctrl; - __u16 buffer[128]; - int result; - - memset(buffer, 0, sizeof(buffer)); - - ctrl.bRequestType = USB_DIR_IN| - USB_TYPE_STANDARD|USB_RECIP_DEVICE; - ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; - ctrl.wValue = (USB_DT_STRING << 8) | dev->iSerialNumber; - ctrl.wIndex = 0; - ctrl.wLength = sizeof(buffer); - ctrl.data = buffer; - ctrl.timeout = 50; - - result = ioctl(fd, USBDEVFS_CONTROL, &ctrl); - if (result > 0) { - int i; - /* skip first word, and copy the rest to the serial - string, changing shorts to bytes. */ - result /= 2; - for (i = 1; i < result; i++) - info.serial_number[i - 1] = buffer[i]; - info.serial_number[i - 1] = 0; - } - } - - for (i = 0; i < cfg->bNumInterfaces; i++) { - if (check(ptr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE)) - return -1; - ifc = (void *) ptr; - len -= ifc->bLength; - ptr += ifc->bLength; - - in = -1; - out = -1; - info.ifc_class = ifc->bInterfaceClass; - info.ifc_subclass = ifc->bInterfaceSubClass; - info.ifc_protocol = ifc->bInterfaceProtocol; - - for (e = 0; e < ifc->bNumEndpoints; e++) { - if (check(ptr, len, USB_DT_ENDPOINT, - USB_DT_ENDPOINT_SIZE) - ) - return -1; - ept = (void *) ptr; - len -= ept->bLength; - ptr += ept->bLength; - - if ((ept->bmAttributes & 0x03) != 0x02) - continue; - - if (ept->bEndpointAddress & 0x80) - in = ept->bEndpointAddress; - else - out = ept->bEndpointAddress; - } - - info.has_bulk_in = (in != -1); - info.has_bulk_out = (out != -1); - - if (callback(&info) == 0) { - *ept_in_id = in; - *ept_out_id = out; - *ifc_id = ifc->bInterfaceNumber; - return 0; - } - } - - return -1; -} - -static struct usb_handle *find_usb_device( - const char *base, ifc_match_func callback) -{ - struct usb_handle *usb = 0; - char busname[64], devname[64]; - char desc[1024]; - int n, in, out, ifc; - - DIR *busdir, *devdir; - struct dirent *de; - int fd; - int writable; - - busdir = opendir(base); - if (busdir == 0) - return 0; - - while ((de = readdir(busdir)) && (usb == 0)) { - if (badname(de->d_name)) - continue; - - sprintf(busname, "%s/%s", base, de->d_name); - devdir = opendir(busname); - if (devdir == 0) - continue; - - /* DBG("[ scanning %s ]\n", busname); */ - while ((de = readdir(devdir)) && (usb == 0)) { - - if (badname(de->d_name)) - continue; - sprintf(devname, "%s/%s", busname, de->d_name); - - /* DBG("[ scanning %s ]\n", devname); */ - writable = 1; - fd = open(devname, O_RDWR); - if (fd < 0) { - /* Check if we have read-only access, - so we can give a helpful diagnostic - like "adb devices" does. */ - writable = 0; - fd = open(devname, O_RDONLY); - if (fd < 0) - continue; - } - - n = read(fd, desc, sizeof(desc)); - - if (filter_usb_device(fd, desc, n, writable, - callback, &in, &out, &ifc) == 0 - ) { - usb = calloc(1, sizeof(struct usb_handle)); - strcpy(usb->fname, devname); - usb->ep_in = in; - usb->ep_out = out; - usb->desc = fd; - - n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); - if (n != 0) { - close(fd); - free(usb); - usb = 0; - continue; - } - } else - close(fd); - } - closedir(devdir); - } - closedir(busdir); - - return usb; -} - -int usb_write(struct usb_handle *h, const void *_data, int len) -{ - unsigned char *data = (unsigned char *) _data; - unsigned count = 0; - struct usbdevfs_bulktransfer bulk; - int n; - - if (h->ep_out == 0) - return -1; - - if (len == 0) { - bulk.ep = h->ep_out; - bulk.len = 0; - bulk.data = data; - bulk.timeout = 0; - - n = ioctl(h->desc, USBDEVFS_BULK, &bulk); - if (n != 0) { - fprintf(stderr, "ERROR: n = %d, errno = %d (%s)\n", - n, errno, strerror(errno)); - return -1; - } - return 0; - } - - while (len > 0) { - int xfer; - xfer = (len > 4096) ? 4096 : len; - - bulk.ep = h->ep_out; - bulk.len = xfer; - bulk.data = data; - bulk.timeout = 0; - - n = ioctl(h->desc, USBDEVFS_BULK, &bulk); - if (n != xfer) { - DBG("ERROR: n = %d, errno = %d (%s)\n", - n, errno, strerror(errno)); - return -1; - } - - count += xfer; - len -= xfer; - data += xfer; - } - - return count; -} - -int usb_read(struct usb_handle *h, void *_data, int len) -{ - unsigned char *data = (unsigned char *) _data; - unsigned count = 0; - struct usbdevfs_bulktransfer bulk; - int n, retry; - - if (h->ep_in == 0) - return -1; - - while (len > 0) { - int xfer = (len > 4096) ? 4096 : len; - - bulk.ep = h->ep_in; - bulk.len = xfer; - bulk.data = data; - bulk.timeout = 0; - retry = 0; - - do { - DBG("[ usb read %d fd = %d], fname=%s\n", - xfer, h->desc, h->fname); - n = ioctl(h->desc, USBDEVFS_BULK, &bulk); - DBG("[ usb read %d ] = %d, fname=%s, Retry %d\n", - xfer, n, h->fname, retry); - - if (n < 0) { - DBG1("ERROR: n = %d, errno = %d (%s)\n", - n, errno, strerror(errno)); - if (++retry > MAX_RETRIES) - return -1; - usleep(10000); - } - } while (n < 0); - - count += n; - len -= n; - data += n; - - if (n < xfer) - break; - } - - return count; -} - -void usb_kick(struct usb_handle *h) -{ - int fd; - - fd = h->desc; - h->desc = -1; - if (fd >= 0) { - close(fd); - DBG("[ usb closed %d ]\n", fd); - } -} - -int usb_close(struct usb_handle *h) -{ - int fd; - - fd = h->desc; - h->desc = -1; - if (fd >= 0) { - close(fd); - DBG("[ usb closed %d ]\n", fd); - } - - return 0; -} - -struct usb_handle *usb_open(ifc_match_func callback) -{ - return find_usb_device("/dev/bus/usb", callback); -}