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)
46 display_pending_firmware(int fd
)
48 struct mfi_ctrl_info info
;
49 struct mfi_info_component header
;
53 if (mfi_ctrl_get_info(fd
, &info
, NULL
) < 0) {
55 warn("Failed to get controller info");
59 printf("mfi%d Pending Firmware Images:\n", mfi_unit
);
60 strcpy(header
.name
, "Name");
61 strcpy(header
.version
, "Version");
62 strcpy(header
.build_date
, "Date");
63 strcpy(header
.build_time
, "Time");
64 scan_firmware(&header
);
65 if (info
.pending_image_component_count
> 8)
66 info
.pending_image_component_count
= 8;
67 for (i
= 0; i
< info
.pending_image_component_count
; i
++)
68 scan_firmware(&info
.pending_image_component
[i
]);
69 display_firmware(&header
, "");
70 for (i
= 0; i
< info
.pending_image_component_count
; i
++)
71 display_firmware(&info
.pending_image_component
[i
], "");
77 mbox_store_word(uint8_t *mbox
, uint32_t val
)
81 mbox
[1] = val
>> 8 & 0xff;
82 mbox
[2] = val
>> 16 & 0xff;
87 flash_adapter(int ac
, char **av
)
89 struct mfi_progress dummy
;
95 uint8_t mbox
[4], status
;
98 warnx("flash: Firmware file required");
102 flash
= open(av
[1], O_RDONLY
);
105 warn("flash: Failed to open %s", av
[1]);
112 if (fstat(flash
, &sb
) < 0) {
114 warn("fstat(%s)", av
[1]);
117 if (sb
.st_size
% 1024 != 0 || sb
.st_size
> 0x7fffffff) {
118 warnx("Invalid flash file size");
123 fd
= mfi_open(mfi_unit
);
130 /* First, ask the firmware to allocate space for the flash file. */
131 mbox_store_word(mbox
, sb
.st_size
);
132 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_OPEN
, NULL
, 0, mbox
, 4, &status
);
133 if (status
!= MFI_STAT_OK
) {
134 warnx("Failed to alloc flash memory: %s", mfi_status(status
));
139 /* Upload the file 64k at a time. */
140 buf
= malloc(FLASH_BUF_SIZE
);
142 warnx("malloc failed");
147 while (sb
.st_size
> 0) {
148 nread
= read(flash
, buf
, FLASH_BUF_SIZE
);
149 if (nread
<= 0 || nread
% 1024 != 0) {
150 warnx("Bad read from flash file");
151 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_CLOSE
, NULL
, 0,
157 mbox_store_word(mbox
, offset
);
158 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_DOWNLOAD
, buf
, nread
,
160 if (status
!= MFI_STAT_OK
) {
161 warnx("Flash download failed: %s", mfi_status(status
));
162 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_CLOSE
, NULL
, 0,
171 /* Kick off the flash. */
172 printf("WARNING: Firmware flash in progress, do not reboot machine... ");
174 mfi_dcmd_command(fd
, MFI_DCMD_FLASH_FW_FLASH
, &dummy
, sizeof(dummy
),
176 if (status
!= MFI_STAT_OK
) {
177 printf("failed:\n\t%s\n", mfi_status(status
));
181 printf("finished\n");
182 error
= display_pending_firmware(fd
);
192 MFI_COMMAND(top
, flash
, flash_adapter
);