/* * Do the final steps of uploading firmware * * @bcf_hdr: BCF header we are actually using * @bcf: pointer to the firmware image (which matches the first header * that is followed by the actual payloads). * @offset: [byte] offset into @bcf for the command we need to send. * * Depending on the boot mode (signed vs non-signed), different * actions need to be taken. */ static int i2400m_dnload_finalize(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf_hdr, const struct i2400m_bcf_hdr *bcf, size_t offset) { int ret = 0; struct device *dev = i2400m_dev(i2400m); struct i2400m_bootrom_header *cmd, ack; struct { struct i2400m_bootrom_header cmd; u8 cmd_pl[0]; } __packed *cmd_buf; size_t signature_block_offset, signature_block_size; d_fnstart(3, dev, "offset %zu\n", offset); cmd = (void *) bcf + offset; if (i2400m_boot_is_signed(i2400m) == 0) { struct i2400m_bootrom_header jump_ack; d_printf(1, dev, "unsecure boot, jumping to 0x%08x\n", le32_to_cpu(cmd->target_addr)); cmd_buf = i2400m->bm_cmd_buf; memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd)); cmd = &cmd_buf->cmd; /* now cmd points to the actual bootrom_header in cmd_buf */ i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP); cmd->data_size = 0; ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), &jump_ack, sizeof(jump_ack), 0); } else { d_printf(1, dev, "secure boot, jumping to 0x%08x\n", le32_to_cpu(cmd->target_addr)); cmd_buf = i2400m->bm_cmd_buf; memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd)); signature_block_offset = sizeof(*bcf_hdr) + le32_to_cpu(bcf_hdr->key_size) * sizeof(u32) + le32_to_cpu(bcf_hdr->exponent_size) * sizeof(u32); signature_block_size = le32_to_cpu(bcf_hdr->modulus_size) * sizeof(u32); memcpy(cmd_buf->cmd_pl, (void *) bcf_hdr + signature_block_offset, signature_block_size); ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(cmd_buf->cmd) + signature_block_size, &ack, sizeof(ack), I2400M_BM_CMD_RAW); } d_fnend(3, dev, "returning %d\n", ret); return ret; }
/** * i2400m_download_chunk - write a single chunk of data to the device's memory * * @i2400m: device descriptor * @buf: the buffer to write * @buf_len: length of the buffer to write * @addr: address in the device memory space * @direct: bootrom write mode * @do_csum: should a checksum validation be performed */ static int i2400m_download_chunk(struct i2400m *i2400m, const void *chunk, size_t __chunk_len, unsigned long addr, unsigned int direct, unsigned int do_csum) { int ret; size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_ALIGN); struct device *dev = i2400m_dev(i2400m); struct { struct i2400m_bootrom_header cmd; u8 cmd_payload[chunk_len]; } __packed *buf; struct i2400m_bootrom_header ack; d_fnstart(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx " "direct %u do_csum %u)\n", i2400m, chunk, __chunk_len, addr, direct, do_csum); buf = i2400m->bm_cmd_buf; memcpy(buf->cmd_payload, chunk, __chunk_len); memset(buf->cmd_payload + __chunk_len, 0xad, chunk_len - __chunk_len); buf->cmd.command = i2400m_brh_command(I2400M_BRH_WRITE, __chunk_len & 0x3 ? 0 : do_csum, __chunk_len & 0xf ? 0 : direct); buf->cmd.target_addr = cpu_to_le32(addr); buf->cmd.data_size = cpu_to_le32(__chunk_len); ret = i2400m_bm_cmd(i2400m, &buf->cmd, sizeof(buf->cmd) + chunk_len, &ack, sizeof(ack), 0); if (ret >= 0) ret = 0; d_fnend(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx " "direct %u do_csum %u) = %d\n", i2400m, chunk, __chunk_len, addr, direct, do_csum, ret); return ret; }
/* * Initialize the signed boot process * * @i2400m: device descriptor * * @bcf_hdr: pointer to the firmware header; assumes it is fully in * memory (it has gone through basic validation). * * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw * rebooted. * * This writes the firmware BCF header to the device using the * HASH_PAYLOAD_ONLY command. */ static int i2400m_dnload_init_signed(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf_hdr) { int ret; struct device *dev = i2400m_dev(i2400m); struct { struct i2400m_bootrom_header cmd; struct i2400m_bcf_hdr cmd_pl; } __packed *cmd_buf; struct i2400m_bootrom_header ack; d_fnstart(5, dev, "(i2400m %p bcf_hdr %p)\n", i2400m, bcf_hdr); cmd_buf = i2400m->bm_cmd_buf; cmd_buf->cmd.command = i2400m_brh_command(I2400M_BRH_HASH_PAYLOAD_ONLY, 0, 0); cmd_buf->cmd.target_addr = 0; cmd_buf->cmd.data_size = cpu_to_le32(sizeof(cmd_buf->cmd_pl)); memcpy(&cmd_buf->cmd_pl, bcf_hdr, sizeof(*bcf_hdr)); ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(*cmd_buf), &ack, sizeof(ack), 0); if (ret >= 0) ret = 0; d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret); return ret; }
/* * Read the MAC addr * * The position this function reads is fixed in device memory and * always available, even without firmware. * * Note we specify we want to read only six bytes, but provide space * for 16, as we always get it rounded up. */ int i2400m_read_mac_addr(struct i2400m *i2400m) { int result; struct device *dev = i2400m_dev(i2400m); struct net_device *net_dev = i2400m->wimax_dev.net_dev; struct i2400m_bootrom_header *cmd; struct { struct i2400m_bootrom_header ack; u8 ack_pl[16]; } __packed ack_buf; d_fnstart(5, dev, "(i2400m %p)\n", i2400m); cmd = i2400m->bm_cmd_buf; cmd->command = i2400m_brh_command(I2400M_BRH_READ, 0, 1); cmd->target_addr = cpu_to_le32(0x00203fe8); cmd->data_size = cpu_to_le32(6); result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), &ack_buf.ack, sizeof(ack_buf), 0); if (result < 0) { dev_err(dev, "BM: read mac addr failed: %d\n", result); goto error_read_mac; } d_printf(2, dev, "mac addr is %pM\n", ack_buf.ack_pl); if (i2400m->bus_bm_mac_addr_impaired == 1) { ack_buf.ack_pl[0] = 0x00; ack_buf.ack_pl[1] = 0x16; ack_buf.ack_pl[2] = 0xd3; get_random_bytes(&ack_buf.ack_pl[3], 3); dev_err(dev, "BM is MAC addr impaired, faking MAC addr to " "mac addr is %pM\n", ack_buf.ack_pl); result = 0; } net_dev->addr_len = ETH_ALEN; memcpy(net_dev->perm_addr, ack_buf.ack_pl, ETH_ALEN); memcpy(net_dev->dev_addr, ack_buf.ack_pl, ETH_ALEN); error_read_mac: d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result); return result; }
/** * i2400m_bootrom_init - Reboots a powered device into boot mode * * @i2400m: device descriptor * @flags: * I2400M_BRI_SOFT: a reboot barker has been seen * already, so don't wait for it. * * I2400M_BRI_NO_REBOOT: Don't send a reboot command, but wait * for a reboot barker notification. This is a one shot; if * the state machine needs to send a reboot command it will. * * Returns: * * < 0 errno code on error, 0 if ok. * * Description: * * Tries hard enough to put the device in boot-mode. There are two * main phases to this: * * a. (1) send a reboot command and (2) get a reboot barker * * b. (1) echo/ack the reboot sending the reboot barker back and (2) * getting an ack barker in return * * We want to skip (a) in some cases [soft]. The state machine is * horrible, but it is basically: on each phase, send what has to be * sent (if any), wait for the answer and act on the answer. We might * have to backtrack and retry, so we keep a max tries counter for * that. * * It sucks because we don't know ahead of time which is going to be * the reboot barker (the device might send different ones depending * on its EEPROM config) and once the device reboots and waits for the * echo/ack reboot barker being sent back, it doesn't understand * anything else. So we can be left at the point where we don't know * what to send to it -- cold reset and bus reset seem to have little * effect. So the function iterates (in this case) through all the * known barkers and tries them all until an ACK is * received. Otherwise, it gives up. * * If we get a timeout after sending a warm reset, we do it again. */ int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags) { int result; struct device *dev = i2400m_dev(i2400m); struct i2400m_bootrom_header *cmd; struct i2400m_bootrom_header ack; int count = i2400m->bus_bm_retries; int ack_timeout_cnt = 1; unsigned i; BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_barker_db[0].data)); BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER)); d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags); result = -ENOMEM; cmd = i2400m->bm_cmd_buf; if (flags & I2400M_BRI_SOFT) goto do_reboot_ack; do_reboot: ack_timeout_cnt = 1; if (--count < 0) goto error_timeout; d_printf(4, dev, "device reboot: reboot command [%d # left]\n", count); if ((flags & I2400M_BRI_NO_REBOOT) == 0) i2400m_reset(i2400m, I2400M_RT_WARM); result = i2400m_bm_cmd(i2400m, NULL, 0, &ack, sizeof(ack), I2400M_BM_CMD_RAW); flags &= ~I2400M_BRI_NO_REBOOT; switch (result) { case -ERESTARTSYS: /* * at this point, i2400m_bm_cmd(), through * __i2400m_bm_ack_process(), has updated * i2400m->barker and we are good to go. */ d_printf(4, dev, "device reboot: got reboot barker\n"); break; case -EISCONN: /* we don't know how it got here...but we follow it */ d_printf(4, dev, "device reboot: got ack barker - whatever\n"); goto do_reboot; case -ETIMEDOUT: /* * Device has timed out, we might be in boot mode * already and expecting an ack; if we don't know what * the barker is, we just send them all. Cold reset * and bus reset don't work. Beats me. */ if (i2400m->barker != NULL) { dev_err(dev, "device boot: reboot barker timed out, " "trying (set) %08x echo/ack\n", le32_to_cpu(i2400m->barker->data[0])); goto do_reboot_ack; } for (i = 0; i < i2400m_barker_db_used; i++) { struct i2400m_barker_db *barker = &i2400m_barker_db[i]; memcpy(cmd, barker->data, sizeof(barker->data)); result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), &ack, sizeof(ack), I2400M_BM_CMD_RAW); if (result == -EISCONN) { dev_warn(dev, "device boot: got ack barker " "after sending echo/ack barker " "#%d/%08x; rebooting j.i.c.\n", i, le32_to_cpu(barker->data[0])); flags &= ~I2400M_BRI_NO_REBOOT; goto do_reboot; } } dev_err(dev, "device boot: tried all the echo/acks, could " "not get device to respond; giving up"); result = -ESHUTDOWN; case -EPROTO: case -ESHUTDOWN: /* dev is gone */ case -EINTR: /* user cancelled */ goto error_dev_gone; default: dev_err(dev, "device reboot: error %d while waiting " "for reboot barker - rebooting\n", result); d_dump(1, dev, &ack, result); goto do_reboot; } /* At this point we ack back with 4 REBOOT barkers and expect * 4 ACK barkers. This is ugly, as we send a raw command -- * hence the cast. _bm_cmd() will catch the reboot ack * notification and report it as -EISCONN. */ do_reboot_ack: d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count); memcpy(cmd, i2400m->barker->data, sizeof(i2400m->barker->data)); result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), &ack, sizeof(ack), I2400M_BM_CMD_RAW); switch (result) { case -ERESTARTSYS: d_printf(4, dev, "reboot ack: got reboot barker - retrying\n"); if (--count < 0) goto error_timeout; goto do_reboot_ack; case -EISCONN: d_printf(4, dev, "reboot ack: got ack barker - good\n"); break; case -ETIMEDOUT: /* no response, maybe it is the other type? */ if (ack_timeout_cnt-- < 0) { d_printf(4, dev, "reboot ack timedout: retrying\n"); goto do_reboot_ack; } else { dev_err(dev, "reboot ack timedout too long: " "trying reboot\n"); goto do_reboot; } break; case -EPROTO: case -ESHUTDOWN: /* dev is gone */ goto error_dev_gone; default: dev_err(dev, "device reboot ack: error %d while waiting for " "reboot ack barker - rebooting\n", result); goto do_reboot; } d_printf(2, dev, "device reboot ack: got ack barker - boot done\n"); result = 0; exit_timeout: error_dev_gone: d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n", i2400m, flags, result); return result; error_timeout: dev_err(dev, "Timed out waiting for reboot ack\n"); result = -ETIMEDOUT; goto exit_timeout; }
/* * Download a BCF file's sections to the device * * @i2400m: device descriptor * @bcf: pointer to firmware data (first header followed by the * payloads). Assumed verified and consistent. * @bcf_len: length (in bytes) of the @bcf buffer. * * Returns: < 0 errno code on error or the offset to the jump instruction. * * Given a BCF file, downloads each section (a command and a payload) * to the device's address space. Actually, it just executes each * command i the BCF file. * * The section size has to be aligned to 4 bytes AND the padding has * to be taken from the firmware file, as the signature takes it into * account. */ static ssize_t i2400m_dnload_bcf(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf, size_t bcf_len) { ssize_t ret; struct device *dev = i2400m_dev(i2400m); size_t offset, /* iterator offset */ data_size, /* Size of the data payload */ section_size, /* Size of the whole section (cmd + payload) */ section = 1; const struct i2400m_bootrom_header *bh; struct i2400m_bootrom_header ack; d_fnstart(3, dev, "(i2400m %p bcf %p bcf_len %zu)\n", i2400m, bcf, bcf_len); /* Iterate over the command blocks in the BCF file that start * after the header */ offset = le32_to_cpu(bcf->header_len) * sizeof(u32); while (1) { /* start sending the file */ bh = (void *) bcf + offset; data_size = le32_to_cpu(bh->data_size); section_size = ALIGN(sizeof(*bh) + data_size, 4); d_printf(7, dev, "downloading section #%zu (@%zu %zu B) to 0x%08x\n", section, offset, sizeof(*bh) + data_size, le32_to_cpu(bh->target_addr)); /* * We look for JUMP cmd from the bootmode header, * either I2400M_BRH_SIGNED_JUMP for secure boot * or I2400M_BRH_JUMP for unsecure boot, the last chunk * should be the bootmode header with JUMP cmd. */ if (i2400m_brh_get_opcode(bh) == I2400M_BRH_SIGNED_JUMP || i2400m_brh_get_opcode(bh) == I2400M_BRH_JUMP) { d_printf(5, dev, "jump found @%zu\n", offset); break; } if (offset + section_size > bcf_len) { dev_err(dev, "fw %s: bad section #%zu, " "end (@%zu) beyond EOF (@%zu)\n", i2400m->fw_name, section, offset + section_size, bcf_len); ret = -EINVAL; goto error_section_beyond_eof; } __i2400m_msleep(20); ret = i2400m_bm_cmd(i2400m, bh, section_size, &ack, sizeof(ack), I2400M_BM_CMD_RAW); if (ret < 0) { dev_err(dev, "fw %s: section #%zu (@%zu %zu B) " "failed %d\n", i2400m->fw_name, section, offset, sizeof(*bh) + data_size, (int) ret); goto error_send; } offset += section_size; section++; } ret = offset; error_section_beyond_eof: error_send: d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n", i2400m, bcf, bcf_len, (int) ret); return ret; }