1 /*- 2 * Copyright (c) 2008, 2009 Yahoo!, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The names of the authors may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/errno.h> 34 #include <sys/stat.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include "mfiutil.h" 42 43 #define FLASH_BUF_SIZE (64 * 1024) 44 45 int fw_name_width, fw_version_width, fw_date_width, fw_time_width; 46 47 static void 48 scan_firmware(struct mfi_info_component *comp) 49 { 50 int len; 51 52 len = strlen(comp->name); 53 if (fw_name_width < len) 54 fw_name_width = len; 55 len = strlen(comp->version); 56 if (fw_version_width < len) 57 fw_version_width = len; 58 len = strlen(comp->build_date); 59 if (fw_date_width < len) 60 fw_date_width = len; 61 len = strlen(comp->build_time); 62 if (fw_time_width < len) 63 fw_time_width = len; 64 } 65 66 static void 67 display_firmware(struct mfi_info_component *comp) 68 { 69 70 printf("%-*s %-*s %-*s %-*s\n", fw_name_width, comp->name, 71 fw_version_width, comp->version, fw_date_width, comp->build_date, 72 fw_time_width, comp->build_time); 73 } 74 75 static void 76 display_pending_firmware(int fd) 77 { 78 struct mfi_ctrl_info info; 79 struct mfi_info_component header; 80 u_int i; 81 82 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) { 83 warn("Failed to get controller info"); 84 return; 85 } 86 87 printf("mfi%d Pending Firmware Images:\n", mfi_unit); 88 strcpy(header.name, "Name"); 89 strcpy(header.version, "Version"); 90 strcpy(header.build_date, "Date"); 91 strcpy(header.build_time, "Time"); 92 scan_firmware(&header); 93 if (info.pending_image_component_count > 8) 94 info.pending_image_component_count = 8; 95 for (i = 0; i < info.pending_image_component_count; i++) 96 scan_firmware(&info.pending_image_component[i]); 97 display_firmware(&header); 98 for (i = 0; i < info.pending_image_component_count; i++) 99 display_firmware(&info.pending_image_component[i]); 100 } 101 102 static void 103 mbox_store_word(uint8_t *mbox, uint32_t val) 104 { 105 106 mbox[0] = val & 0xff; 107 mbox[1] = val >> 8 & 0xff; 108 mbox[2] = val >> 16 & 0xff; 109 mbox[3] = val >> 24; 110 } 111 112 static int 113 flash_adapter(int ac, char **av) 114 { 115 struct mfi_progress dummy; 116 off_t offset; 117 size_t nread; 118 char *buf; 119 struct stat sb; 120 int fd, flash; 121 uint8_t mbox[4], status; 122 123 if (ac != 2) { 124 warnx("flash: Firmware file required"); 125 return (EINVAL); 126 } 127 128 flash = open(av[1], O_RDONLY); 129 if (flash < 0) { 130 warn("flash: Failed to open %s", av[1]); 131 return (errno); 132 } 133 134 if (fstat(flash, &sb) < 0) { 135 warn("fstat(%s)", av[1]); 136 return (errno); 137 } 138 if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) { 139 warnx("Invalid flash file size"); 140 return (EINVAL); 141 } 142 143 fd = mfi_open(mfi_unit); 144 if (fd < 0) { 145 warn("mfi_open"); 146 return (errno); 147 } 148 149 /* First, ask the firmware to allocate space for the flash file. */ 150 mbox_store_word(mbox, sb.st_size); 151 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, &status); 152 if (status != MFI_STAT_OK) { 153 warnx("Failed to alloc flash memory: %s", mfi_status(status)); 154 return (EIO); 155 } 156 157 /* Upload the file 64k at a time. */ 158 buf = malloc(FLASH_BUF_SIZE); 159 offset = 0; 160 while (sb.st_size > 0) { 161 nread = read(flash, buf, FLASH_BUF_SIZE); 162 if (nread <= 0 || nread % 1024 != 0) { 163 warnx("Bad read from flash file"); 164 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0, 165 NULL, 0, NULL); 166 return (ENXIO); 167 } 168 169 mbox_store_word(mbox, offset); 170 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread, 171 mbox, 4, &status); 172 if (status != MFI_STAT_OK) { 173 warnx("Flash download failed: %s", mfi_status(status)); 174 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0, 175 NULL, 0, NULL); 176 return (ENXIO); 177 } 178 sb.st_size -= nread; 179 offset += nread; 180 } 181 close(flash); 182 183 /* Kick off the flash. */ 184 printf("WARNING: Firmware flash in progress, do not reboot machine... "); 185 fflush(stdout); 186 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy), 187 NULL, 0, &status); 188 if (status != MFI_STAT_OK) { 189 printf("failed:\n\t%s\n", mfi_status(status)); 190 return (ENXIO); 191 } 192 printf("finished\n"); 193 display_pending_firmware(fd); 194 195 close(fd); 196 197 return (0); 198 } 199 MFI_COMMAND(top, flash, flash_adapter); 200