2 * Copyright (c) 2008, 2009 Yahoo!, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
32 #include <sys/param.h>
33 #include <sys/errno.h>
43 #define FLASH_BUF_SIZE (64 * 1024)
45 int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
48 scan_firmware(struct mfi_info_component *comp)
52 len = strlen(comp->name);
53 if (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)
61 len = strlen(comp->build_time);
62 if (fw_time_width < len)
67 display_firmware(struct mfi_info_component *comp)
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);
76 display_pending_firmware(int fd)
78 struct mfi_ctrl_info info;
79 struct mfi_info_component header;
82 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
83 warn("Failed to get controller info");
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]);
103 mbox_store_word(uint8_t *mbox, uint32_t val)
106 mbox[0] = val & 0xff;
107 mbox[1] = val >> 8 & 0xff;
108 mbox[2] = val >> 16 & 0xff;
113 flash_adapter(int ac, char **av)
115 struct mfi_progress dummy;
121 uint8_t mbox[4], status;
124 warnx("flash: Firmware file required");
128 flash = open(av[1], O_RDONLY);
130 warn("flash: Failed to open %s", av[1]);
134 if (fstat(flash, &sb) < 0) {
135 warn("fstat(%s)", av[1]);
138 if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) {
139 warnx("Invalid flash file size");
143 fd = mfi_open(mfi_unit);
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));
157 /* Upload the file 64k at a time. */
158 buf = malloc(FLASH_BUF_SIZE);
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,
169 mbox_store_word(mbox, offset);
170 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
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,
183 /* Kick off the flash. */
184 printf("WARNING: Firmware flash in progress, do not reboot machine... ");
186 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
188 if (status != MFI_STAT_OK) {
189 printf("failed:\n\t%s\n", mfi_status(status));
192 printf("finished\n");
193 display_pending_firmware(fd);
199 MFI_COMMAND(top, flash, flash_adapter);