Deleted Added
full compact
mfi_flash.c (214396) mfi_flash.c (215526)
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 *
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: head/usr.sbin/mfiutil/mfi_flash.c 214396 2010-10-26 19:11:09Z jhb $
29 * $FreeBSD: head/usr.sbin/mfiutil/mfi_flash.c 215526 2010-11-19 15:39:59Z jhb $
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
45int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
46
47static void
48scan_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
66static void
67display_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
75static int
76display_pending_firmware(int fd)
77{
78 struct mfi_ctrl_info info;
79 struct mfi_info_component header;
80 int error;
81 u_int i;
82
83 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
84 error = errno;
85 warn("Failed to get controller info");
86 return (error);
87 }
88
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]);
102
103 return (0);
104}
105
106static void
107mbox_store_word(uint8_t *mbox, uint32_t val)
108{
109
110 mbox[0] = val & 0xff;
111 mbox[1] = val >> 8 & 0xff;
112 mbox[2] = val >> 16 & 0xff;
113 mbox[3] = val >> 24;
114}
115
116static int
117flash_adapter(int ac, char **av)
118{
119 struct mfi_progress dummy;
120 off_t offset;
121 size_t nread;
122 char *buf;
123 struct stat sb;
124 int error, fd, flash;
125 uint8_t mbox[4], status;
126
127 if (ac != 2) {
128 warnx("flash: Firmware file required");
129 return (EINVAL);
130 }
131
132 flash = open(av[1], O_RDONLY);
133 if (flash < 0) {
134 error = errno;
135 warn("flash: Failed to open %s", av[1]);
136 return (error);
137 }
138
139 if (fstat(flash, &sb) < 0) {
140 error = errno;
141 warn("fstat(%s)", av[1]);
142 return (error);
143 }
144 if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) {
145 warnx("Invalid flash file size");
146 return (EINVAL);
147 }
148
149 fd = mfi_open(mfi_unit);
150 if (fd < 0) {
151 error = errno;
152 warn("mfi_open");
153 return (error);
154 }
155
156 /* First, ask the firmware to allocate space for the flash file. */
157 mbox_store_word(mbox, sb.st_size);
158 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, &status);
159 if (status != MFI_STAT_OK) {
160 warnx("Failed to alloc flash memory: %s", mfi_status(status));
161 return (EIO);
162 }
163
164 /* Upload the file 64k at a time. */
165 buf = malloc(FLASH_BUF_SIZE);
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
45int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
46
47static void
48scan_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
66static void
67display_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
75static int
76display_pending_firmware(int fd)
77{
78 struct mfi_ctrl_info info;
79 struct mfi_info_component header;
80 int error;
81 u_int i;
82
83 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
84 error = errno;
85 warn("Failed to get controller info");
86 return (error);
87 }
88
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]);
102
103 return (0);
104}
105
106static void
107mbox_store_word(uint8_t *mbox, uint32_t val)
108{
109
110 mbox[0] = val & 0xff;
111 mbox[1] = val >> 8 & 0xff;
112 mbox[2] = val >> 16 & 0xff;
113 mbox[3] = val >> 24;
114}
115
116static int
117flash_adapter(int ac, char **av)
118{
119 struct mfi_progress dummy;
120 off_t offset;
121 size_t nread;
122 char *buf;
123 struct stat sb;
124 int error, fd, flash;
125 uint8_t mbox[4], status;
126
127 if (ac != 2) {
128 warnx("flash: Firmware file required");
129 return (EINVAL);
130 }
131
132 flash = open(av[1], O_RDONLY);
133 if (flash < 0) {
134 error = errno;
135 warn("flash: Failed to open %s", av[1]);
136 return (error);
137 }
138
139 if (fstat(flash, &sb) < 0) {
140 error = errno;
141 warn("fstat(%s)", av[1]);
142 return (error);
143 }
144 if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) {
145 warnx("Invalid flash file size");
146 return (EINVAL);
147 }
148
149 fd = mfi_open(mfi_unit);
150 if (fd < 0) {
151 error = errno;
152 warn("mfi_open");
153 return (error);
154 }
155
156 /* First, ask the firmware to allocate space for the flash file. */
157 mbox_store_word(mbox, sb.st_size);
158 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, &status);
159 if (status != MFI_STAT_OK) {
160 warnx("Failed to alloc flash memory: %s", mfi_status(status));
161 return (EIO);
162 }
163
164 /* Upload the file 64k at a time. */
165 buf = malloc(FLASH_BUF_SIZE);
166 if (buf == NULL) {
167 warnx("malloc failed");
168 return (ENOMEM);
169 }
166 offset = 0;
167 while (sb.st_size > 0) {
168 nread = read(flash, buf, FLASH_BUF_SIZE);
169 if (nread <= 0 || nread % 1024 != 0) {
170 warnx("Bad read from flash file");
171 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
172 NULL, 0, NULL);
173 return (ENXIO);
174 }
175
176 mbox_store_word(mbox, offset);
177 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
178 mbox, 4, &status);
179 if (status != MFI_STAT_OK) {
180 warnx("Flash download failed: %s", mfi_status(status));
181 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
182 NULL, 0, NULL);
183 return (ENXIO);
184 }
185 sb.st_size -= nread;
186 offset += nread;
187 }
188 close(flash);
189
190 /* Kick off the flash. */
191 printf("WARNING: Firmware flash in progress, do not reboot machine... ");
192 fflush(stdout);
193 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
194 NULL, 0, &status);
195 if (status != MFI_STAT_OK) {
196 printf("failed:\n\t%s\n", mfi_status(status));
197 return (ENXIO);
198 }
199 printf("finished\n");
200 error = display_pending_firmware(fd);
201
202 close(fd);
203
204 return (error);
205}
206MFI_COMMAND(top, flash, flash_adapter);
170 offset = 0;
171 while (sb.st_size > 0) {
172 nread = read(flash, buf, FLASH_BUF_SIZE);
173 if (nread <= 0 || nread % 1024 != 0) {
174 warnx("Bad read from flash file");
175 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
176 NULL, 0, NULL);
177 return (ENXIO);
178 }
179
180 mbox_store_word(mbox, offset);
181 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
182 mbox, 4, &status);
183 if (status != MFI_STAT_OK) {
184 warnx("Flash download failed: %s", mfi_status(status));
185 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
186 NULL, 0, NULL);
187 return (ENXIO);
188 }
189 sb.st_size -= nread;
190 offset += nread;
191 }
192 close(flash);
193
194 /* Kick off the flash. */
195 printf("WARNING: Firmware flash in progress, do not reboot machine... ");
196 fflush(stdout);
197 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
198 NULL, 0, &status);
199 if (status != MFI_STAT_OK) {
200 printf("failed:\n\t%s\n", mfi_status(status));
201 return (ENXIO);
202 }
203 printf("finished\n");
204 error = display_pending_firmware(fd);
205
206 close(fd);
207
208 return (error);
209}
210MFI_COMMAND(top, flash, flash_adapter);