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
29 * $FreeBSD: src/usr.sbin/mfiutil/mfi_flash.c,v 1.4 2011/06/09 19:52:28 bz Exp $
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
;
83 if (mfi_ctrl_get_info(fd
, &info
, NULL
) < 0) {
85 warn("Failed to get controller info");
89 printf("mfi%d Pending Firmware Images:\n", mfi_unit
);
90 strcpy(header
.name
, "Name");
91 strcpy(header
.version
, "Version");
92 strcpy(header
.build_date
, "Date");
93 strcpy(header
.build_time
, "Time");
94 scan_firmware(&header
);
95 if (info
.pending_image_component_count
> 8)
96 info
.pending_image_component_count
= 8;
97 for (i
= 0; i
< info
.pending_image_component_count
; i
++)
98 scan_firmware(&info
.pending_image_component
[i
]);
99 display_firmware(&header
);
100 for (i
= 0; i
< info
.pending_image_component_count
; i
++)
101 display_firmware(&info
.pending_image_component
[i
]);
107 mbox_store_word(uint8_t *mbox
, uint32_t val
)
110 mbox
[0] = val
& 0xff;
111 mbox
[1] = val
>> 8 & 0xff;
112 mbox
[2] = val
>> 16 & 0xff;
117 flash_adapter(int ac
, char **av
)
119 struct mfi_progress dummy
;
124 int error
, fd
, flash
;
125 uint8_t mbox
[4], status
;
128 warnx("flash: Firmware file required");
132 flash
= open(av
[1], O_RDONLY
);
135 warn("flash: Failed to open %s", av
[1]);
142 if (fstat(flash
, &sb
) < 0) {
144 warn("fstat(%s)", av
[1]);
147 if (sb
.st_size
% 1024 != 0 || sb
.st_size
> 0x7fffffff) {
148 warnx("Invalid flash file size");
153 fd
= mfi_open(mfi_unit
);
160 /* First, ask the firmware to allocate space for the flash file. */
161 mbox_store_word(mbox
, sb
.st_size
);
162 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_OPEN
, NULL
, 0, mbox
, 4, &status
);
163 if (status
!= MFI_STAT_OK
) {
164 warnx("Failed to alloc flash memory: %s", mfi_status(status
));
169 /* Upload the file 64k at a time. */
170 buf
= malloc(FLASH_BUF_SIZE
);
172 warnx("malloc failed");
177 while (sb
.st_size
> 0) {
178 nread
= read(flash
, buf
, FLASH_BUF_SIZE
);
179 if (nread
<= 0 || nread
% 1024 != 0) {
180 warnx("Bad read from flash file");
181 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_CLOSE
, NULL
, 0,
187 mbox_store_word(mbox
, offset
);
188 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_DOWNLOAD
, buf
, nread
,
190 if (status
!= MFI_STAT_OK
) {
191 warnx("Flash download failed: %s", mfi_status(status
));
192 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_CLOSE
, NULL
, 0,
201 /* Kick off the flash. */
202 printf("WARNING: Firmware flash in progress, do not reboot machine... ");
204 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_FLASH
, &dummy
, sizeof(dummy
),
206 if (status
!= MFI_STAT_OK
) {
207 printf("failed:\n\t%s\n", mfi_status(status
));
211 printf("finished\n");
212 error
= display_pending_firmware(fd
);
222 MFI_COMMAND(top
, flash
, flash_adapter
);