Merge branch 'vendor/DHCPCD'
[dragonfly.git] / usr.sbin / mfiutil / mfi_flash.c
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: src/usr.sbin/mfiutil/mfi_flash.c,v 1.4 2011/06/09 19:52:28 bz Exp $
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 int
76 display_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
106 static void
107 mbox_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
116 static int
117 flash_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         buf = NULL;
140         fd = -1;
141
142         if (fstat(flash, &sb) < 0) {
143                 error = errno;
144                 warn("fstat(%s)", av[1]);
145                 goto error;
146         }
147         if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) {
148                 warnx("Invalid flash file size");
149                 error = EINVAL;
150                 goto error;
151         }
152
153         fd = mfi_open(mfi_unit);
154         if (fd < 0) {
155                 error = errno;
156                 warn("mfi_open");
157                 goto error;
158         }
159
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));
165                 error = EIO;
166                 goto error;
167         }
168
169         /* Upload the file 64k at a time. */
170         buf = malloc(FLASH_BUF_SIZE);
171         if (buf == NULL) {
172                 warnx("malloc failed");
173                 error = ENOMEM;
174                 goto error;
175         }
176         offset = 0;
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,
182                             NULL, 0, NULL);
183                         error = ENXIO;
184                         goto error;
185                 }
186
187                 mbox_store_word(mbox, offset);
188                 mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
189                     mbox, 4, &status);
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,
193                             NULL, 0, NULL);
194                         error = ENXIO;
195                         goto error;
196                 }
197                 sb.st_size -= nread;
198                 offset += nread;
199         }
200
201         /* Kick off the flash. */
202         printf("WARNING: Firmware flash in progress, do not reboot machine... ");
203         fflush(stdout);
204         mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
205             NULL, 0, &status);
206         if (status != MFI_STAT_OK) {
207                 printf("failed:\n\t%s\n", mfi_status(status));
208                 error = ENXIO;
209                 goto error;
210         }
211         printf("finished\n");
212         error = display_pending_firmware(fd);
213
214 error:
215         free(buf);
216         if (fd >= 0)
217                 close(fd);
218         close(flash);
219
220         return (error);
221 }
222 MFI_COMMAND(top, flash, flash_adapter);