1763fae79SScott Long /*- 2763fae79SScott Long * Copyright (c) 2008, 2009 Yahoo!, Inc. 3763fae79SScott Long * All rights reserved. 4763fae79SScott Long * 5763fae79SScott Long * Redistribution and use in source and binary forms, with or without 6763fae79SScott Long * modification, are permitted provided that the following conditions 7763fae79SScott Long * are met: 8763fae79SScott Long * 1. Redistributions of source code must retain the above copyright 9763fae79SScott Long * notice, this list of conditions and the following disclaimer. 10763fae79SScott Long * 2. Redistributions in binary form must reproduce the above copyright 11763fae79SScott Long * notice, this list of conditions and the following disclaimer in the 12763fae79SScott Long * documentation and/or other materials provided with the distribution. 13763fae79SScott Long * 3. The names of the authors may not be used to endorse or promote 14763fae79SScott Long * products derived from this software without specific prior written 15763fae79SScott Long * permission. 16763fae79SScott Long * 17763fae79SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18763fae79SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19763fae79SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20763fae79SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21763fae79SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22763fae79SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23763fae79SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24763fae79SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25763fae79SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26763fae79SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27763fae79SScott Long * SUCH DAMAGE. 28763fae79SScott Long * 29763fae79SScott Long * $FreeBSD$ 30763fae79SScott Long */ 31763fae79SScott Long 32763fae79SScott Long #include <sys/param.h> 33763fae79SScott Long #include <sys/errno.h> 34763fae79SScott Long #include <sys/stat.h> 35763fae79SScott Long #include <err.h> 36763fae79SScott Long #include <fcntl.h> 37763fae79SScott Long #include <stdio.h> 38763fae79SScott Long #include <stdlib.h> 39763fae79SScott Long #include <string.h> 40763fae79SScott Long #include <unistd.h> 41763fae79SScott Long #include "mfiutil.h" 42763fae79SScott Long 43763fae79SScott Long #define FLASH_BUF_SIZE (64 * 1024) 44763fae79SScott Long 45c02999d9SJohn Baldwin static int 46763fae79SScott Long display_pending_firmware(int fd) 47763fae79SScott Long { 48763fae79SScott Long struct mfi_ctrl_info info; 49763fae79SScott Long struct mfi_info_component header; 50c02999d9SJohn Baldwin int error; 51763fae79SScott Long u_int i; 52763fae79SScott Long 53763fae79SScott Long if (mfi_ctrl_get_info(fd, &info, NULL) < 0) { 54c02999d9SJohn Baldwin error = errno; 55763fae79SScott Long warn("Failed to get controller info"); 56c02999d9SJohn Baldwin return (error); 57763fae79SScott Long } 58763fae79SScott Long 59763fae79SScott Long printf("mfi%d Pending Firmware Images:\n", mfi_unit); 60763fae79SScott Long strcpy(header.name, "Name"); 61763fae79SScott Long strcpy(header.version, "Version"); 62763fae79SScott Long strcpy(header.build_date, "Date"); 63763fae79SScott Long strcpy(header.build_time, "Time"); 64763fae79SScott Long scan_firmware(&header); 65763fae79SScott Long if (info.pending_image_component_count > 8) 66763fae79SScott Long info.pending_image_component_count = 8; 67763fae79SScott Long for (i = 0; i < info.pending_image_component_count; i++) 68763fae79SScott Long scan_firmware(&info.pending_image_component[i]); 69*186475e2SEd Schouten display_firmware(&header, ""); 70763fae79SScott Long for (i = 0; i < info.pending_image_component_count; i++) 71*186475e2SEd Schouten display_firmware(&info.pending_image_component[i], ""); 72c02999d9SJohn Baldwin 73c02999d9SJohn Baldwin return (0); 74763fae79SScott Long } 75763fae79SScott Long 76763fae79SScott Long static void 77763fae79SScott Long mbox_store_word(uint8_t *mbox, uint32_t val) 78763fae79SScott Long { 79763fae79SScott Long 80763fae79SScott Long mbox[0] = val & 0xff; 81763fae79SScott Long mbox[1] = val >> 8 & 0xff; 82763fae79SScott Long mbox[2] = val >> 16 & 0xff; 83763fae79SScott Long mbox[3] = val >> 24; 84763fae79SScott Long } 85763fae79SScott Long 86763fae79SScott Long static int 87763fae79SScott Long flash_adapter(int ac, char **av) 88763fae79SScott Long { 89763fae79SScott Long struct mfi_progress dummy; 90763fae79SScott Long off_t offset; 91763fae79SScott Long size_t nread; 92763fae79SScott Long char *buf; 93763fae79SScott Long struct stat sb; 94c02999d9SJohn Baldwin int error, fd, flash; 95763fae79SScott Long uint8_t mbox[4], status; 96763fae79SScott Long 97763fae79SScott Long if (ac != 2) { 98763fae79SScott Long warnx("flash: Firmware file required"); 99763fae79SScott Long return (EINVAL); 100763fae79SScott Long } 101763fae79SScott Long 102763fae79SScott Long flash = open(av[1], O_RDONLY); 103763fae79SScott Long if (flash < 0) { 104c02999d9SJohn Baldwin error = errno; 105763fae79SScott Long warn("flash: Failed to open %s", av[1]); 106c02999d9SJohn Baldwin return (error); 107763fae79SScott Long } 108763fae79SScott Long 109375c4656SBjoern A. Zeeb buf = NULL; 110375c4656SBjoern A. Zeeb fd = -1; 111375c4656SBjoern A. Zeeb 112763fae79SScott Long if (fstat(flash, &sb) < 0) { 113c02999d9SJohn Baldwin error = errno; 114763fae79SScott Long warn("fstat(%s)", av[1]); 115375c4656SBjoern A. Zeeb goto error; 116763fae79SScott Long } 117763fae79SScott Long if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) { 118763fae79SScott Long warnx("Invalid flash file size"); 119375c4656SBjoern A. Zeeb error = EINVAL; 120375c4656SBjoern A. Zeeb goto error; 121763fae79SScott Long } 122763fae79SScott Long 123bf4ec4dfSEitan Adler fd = mfi_open(mfi_unit, O_RDWR); 124763fae79SScott Long if (fd < 0) { 125c02999d9SJohn Baldwin error = errno; 126763fae79SScott Long warn("mfi_open"); 127375c4656SBjoern A. Zeeb goto error; 128763fae79SScott Long } 129763fae79SScott Long 130763fae79SScott Long /* First, ask the firmware to allocate space for the flash file. */ 131763fae79SScott Long mbox_store_word(mbox, sb.st_size); 132763fae79SScott Long mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, &status); 133763fae79SScott Long if (status != MFI_STAT_OK) { 134763fae79SScott Long warnx("Failed to alloc flash memory: %s", mfi_status(status)); 135375c4656SBjoern A. Zeeb error = EIO; 136375c4656SBjoern A. Zeeb goto error; 137763fae79SScott Long } 138763fae79SScott Long 139763fae79SScott Long /* Upload the file 64k at a time. */ 140763fae79SScott Long buf = malloc(FLASH_BUF_SIZE); 1413c22a809SJohn Baldwin if (buf == NULL) { 1423c22a809SJohn Baldwin warnx("malloc failed"); 143375c4656SBjoern A. Zeeb error = ENOMEM; 144375c4656SBjoern A. Zeeb goto error; 1453c22a809SJohn Baldwin } 146763fae79SScott Long offset = 0; 147763fae79SScott Long while (sb.st_size > 0) { 148763fae79SScott Long nread = read(flash, buf, FLASH_BUF_SIZE); 149763fae79SScott Long if (nread <= 0 || nread % 1024 != 0) { 150763fae79SScott Long warnx("Bad read from flash file"); 151763fae79SScott Long mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0, 152763fae79SScott Long NULL, 0, NULL); 153375c4656SBjoern A. Zeeb error = ENXIO; 154375c4656SBjoern A. Zeeb goto error; 155763fae79SScott Long } 156763fae79SScott Long 157763fae79SScott Long mbox_store_word(mbox, offset); 158763fae79SScott Long mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread, 159763fae79SScott Long mbox, 4, &status); 160763fae79SScott Long if (status != MFI_STAT_OK) { 161763fae79SScott Long warnx("Flash download failed: %s", mfi_status(status)); 162763fae79SScott Long mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0, 163763fae79SScott Long NULL, 0, NULL); 164375c4656SBjoern A. Zeeb error = ENXIO; 165375c4656SBjoern A. Zeeb goto error; 166763fae79SScott Long } 167763fae79SScott Long sb.st_size -= nread; 168763fae79SScott Long offset += nread; 169763fae79SScott Long } 170763fae79SScott Long 171763fae79SScott Long /* Kick off the flash. */ 172763fae79SScott Long printf("WARNING: Firmware flash in progress, do not reboot machine... "); 173763fae79SScott Long fflush(stdout); 174763fae79SScott Long mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy), 175763fae79SScott Long NULL, 0, &status); 176763fae79SScott Long if (status != MFI_STAT_OK) { 177763fae79SScott Long printf("failed:\n\t%s\n", mfi_status(status)); 178375c4656SBjoern A. Zeeb error = ENXIO; 179375c4656SBjoern A. Zeeb goto error; 180763fae79SScott Long } 181763fae79SScott Long printf("finished\n"); 182c02999d9SJohn Baldwin error = display_pending_firmware(fd); 183763fae79SScott Long 184375c4656SBjoern A. Zeeb error: 185375c4656SBjoern A. Zeeb free(buf); 186375c4656SBjoern A. Zeeb if (fd >= 0) 187763fae79SScott Long close(fd); 188375c4656SBjoern A. Zeeb close(flash); 189763fae79SScott Long 190c02999d9SJohn Baldwin return (error); 191763fae79SScott Long } 192763fae79SScott Long MFI_COMMAND(top, flash, flash_adapter); 193