1763fae79SScott Long /*-
21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
31de7b4b8SPedro F. Giffuni *
4763fae79SScott Long * Copyright (c) 2008, 2009 Yahoo!, Inc.
5763fae79SScott Long * All rights reserved.
6763fae79SScott Long *
7763fae79SScott Long * Redistribution and use in source and binary forms, with or without
8763fae79SScott Long * modification, are permitted provided that the following conditions
9763fae79SScott Long * are met:
10763fae79SScott Long * 1. Redistributions of source code must retain the above copyright
11763fae79SScott Long * notice, this list of conditions and the following disclaimer.
12763fae79SScott Long * 2. Redistributions in binary form must reproduce the above copyright
13763fae79SScott Long * notice, this list of conditions and the following disclaimer in the
14763fae79SScott Long * documentation and/or other materials provided with the distribution.
15763fae79SScott Long * 3. The names of the authors may not be used to endorse or promote
16763fae79SScott Long * products derived from this software without specific prior written
17763fae79SScott Long * permission.
18763fae79SScott Long *
19763fae79SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20763fae79SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21763fae79SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22763fae79SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23763fae79SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24763fae79SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25763fae79SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26763fae79SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27763fae79SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28763fae79SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29763fae79SScott Long * SUCH DAMAGE.
30763fae79SScott Long */
31763fae79SScott Long
32763fae79SScott Long #include <sys/param.h>
33763fae79SScott Long #include <sys/errno.h>
34763fae79SScott Long #include <sys/stat.h>
35763fae79SScott Long #include <err.h>
36763fae79SScott Long #include <fcntl.h>
37763fae79SScott Long #include <stdio.h>
38763fae79SScott Long #include <stdlib.h>
39763fae79SScott Long #include <string.h>
40763fae79SScott Long #include <unistd.h>
41763fae79SScott Long #include "mfiutil.h"
42763fae79SScott Long
43763fae79SScott Long #define FLASH_BUF_SIZE (64 * 1024)
44763fae79SScott Long
45c02999d9SJohn Baldwin static int
display_pending_firmware(int fd)46763fae79SScott Long display_pending_firmware(int fd)
47763fae79SScott Long {
48763fae79SScott Long struct mfi_ctrl_info info;
49763fae79SScott Long struct mfi_info_component header;
50c02999d9SJohn Baldwin int error;
51763fae79SScott Long u_int i;
52763fae79SScott Long
53763fae79SScott Long if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
54c02999d9SJohn Baldwin error = errno;
55763fae79SScott Long warn("Failed to get controller info");
56c02999d9SJohn Baldwin return (error);
57763fae79SScott Long }
58763fae79SScott Long
597e0f8b79SDoug Ambrisko printf("%s Pending Firmware Images:\n", mfi_device);
60763fae79SScott Long strcpy(header.name, "Name");
61763fae79SScott Long strcpy(header.version, "Version");
62763fae79SScott Long strcpy(header.build_date, "Date");
63763fae79SScott Long strcpy(header.build_time, "Time");
64763fae79SScott Long scan_firmware(&header);
65763fae79SScott Long if (info.pending_image_component_count > 8)
66763fae79SScott Long info.pending_image_component_count = 8;
67763fae79SScott Long for (i = 0; i < info.pending_image_component_count; i++)
68763fae79SScott Long scan_firmware(&info.pending_image_component[i]);
69186475e2SEd Schouten display_firmware(&header, "");
70763fae79SScott Long for (i = 0; i < info.pending_image_component_count; i++)
71186475e2SEd Schouten display_firmware(&info.pending_image_component[i], "");
72c02999d9SJohn Baldwin
73c02999d9SJohn Baldwin return (0);
74763fae79SScott Long }
75763fae79SScott Long
76763fae79SScott Long static void
mbox_store_word(uint8_t * mbox,uint32_t val)77763fae79SScott Long mbox_store_word(uint8_t *mbox, uint32_t val)
78763fae79SScott Long {
79763fae79SScott Long
80763fae79SScott Long mbox[0] = val & 0xff;
81763fae79SScott Long mbox[1] = val >> 8 & 0xff;
82763fae79SScott Long mbox[2] = val >> 16 & 0xff;
83763fae79SScott Long mbox[3] = val >> 24;
84763fae79SScott Long }
85763fae79SScott Long
86763fae79SScott Long static int
flash_adapter(int ac,char ** av)87763fae79SScott Long flash_adapter(int ac, char **av)
88763fae79SScott Long {
89763fae79SScott Long struct mfi_progress dummy;
90763fae79SScott Long off_t offset;
91763fae79SScott Long size_t nread;
92763fae79SScott Long char *buf;
93763fae79SScott Long struct stat sb;
94c02999d9SJohn Baldwin int error, fd, flash;
95763fae79SScott Long uint8_t mbox[4], status;
96763fae79SScott Long
97763fae79SScott Long if (ac != 2) {
98763fae79SScott Long warnx("flash: Firmware file required");
99763fae79SScott Long return (EINVAL);
100763fae79SScott Long }
101763fae79SScott Long
102763fae79SScott Long flash = open(av[1], O_RDONLY);
103763fae79SScott Long if (flash < 0) {
104c02999d9SJohn Baldwin error = errno;
105763fae79SScott Long warn("flash: Failed to open %s", av[1]);
106c02999d9SJohn Baldwin return (error);
107763fae79SScott Long }
108763fae79SScott Long
109375c4656SBjoern A. Zeeb buf = NULL;
110375c4656SBjoern A. Zeeb fd = -1;
111375c4656SBjoern A. Zeeb
112763fae79SScott Long if (fstat(flash, &sb) < 0) {
113c02999d9SJohn Baldwin error = errno;
114763fae79SScott Long warn("fstat(%s)", av[1]);
115375c4656SBjoern A. Zeeb goto error;
116763fae79SScott Long }
117763fae79SScott Long if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) {
118763fae79SScott Long warnx("Invalid flash file size");
119375c4656SBjoern A. Zeeb error = EINVAL;
120375c4656SBjoern A. Zeeb goto error;
121763fae79SScott Long }
122763fae79SScott Long
1237e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDWR);
124763fae79SScott Long if (fd < 0) {
125c02999d9SJohn Baldwin error = errno;
126763fae79SScott Long warn("mfi_open");
127375c4656SBjoern A. Zeeb goto error;
128763fae79SScott Long }
129763fae79SScott Long
130763fae79SScott Long /* First, ask the firmware to allocate space for the flash file. */
131763fae79SScott Long mbox_store_word(mbox, sb.st_size);
132*bac98f86SWHR if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4,
133*bac98f86SWHR &status) < 0) {
134*bac98f86SWHR error = errno;
135*bac98f86SWHR warn("Failed to allocate flash memory");
136*bac98f86SWHR goto error;
137*bac98f86SWHR }
138763fae79SScott Long if (status != MFI_STAT_OK) {
139*bac98f86SWHR warnx("Failed to allocate flash memory: %s",
140*bac98f86SWHR mfi_status(status));
141375c4656SBjoern A. Zeeb error = EIO;
142375c4656SBjoern A. Zeeb goto error;
143763fae79SScott Long }
144763fae79SScott Long
145763fae79SScott Long /* Upload the file 64k at a time. */
146763fae79SScott Long buf = malloc(FLASH_BUF_SIZE);
1473c22a809SJohn Baldwin if (buf == NULL) {
1483c22a809SJohn Baldwin warnx("malloc failed");
149375c4656SBjoern A. Zeeb error = ENOMEM;
150375c4656SBjoern A. Zeeb goto error;
1513c22a809SJohn Baldwin }
152763fae79SScott Long offset = 0;
153763fae79SScott Long while (sb.st_size > 0) {
154763fae79SScott Long nread = read(flash, buf, FLASH_BUF_SIZE);
155763fae79SScott Long if (nread <= 0 || nread % 1024 != 0) {
156763fae79SScott Long warnx("Bad read from flash file");
157*bac98f86SWHR if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE,
158*bac98f86SWHR NULL, 0, NULL, 0, NULL) < 0) {
159*bac98f86SWHR warn("Failed to discard flash memory");
160*bac98f86SWHR }
161375c4656SBjoern A. Zeeb error = ENXIO;
162375c4656SBjoern A. Zeeb goto error;
163763fae79SScott Long }
164763fae79SScott Long
165763fae79SScott Long mbox_store_word(mbox, offset);
166*bac98f86SWHR if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
167*bac98f86SWHR mbox, 4, &status) < 0) {
168*bac98f86SWHR error = errno;
169*bac98f86SWHR warn("Failed to download firmware");
170*bac98f86SWHR goto error;
171*bac98f86SWHR }
172763fae79SScott Long if (status != MFI_STAT_OK) {
173*bac98f86SWHR warnx("Failed to download firmware: %s",
174*bac98f86SWHR mfi_status(status));
175*bac98f86SWHR mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL,
176*bac98f86SWHR 0, NULL, 0, NULL);
177375c4656SBjoern A. Zeeb error = ENXIO;
178375c4656SBjoern A. Zeeb goto error;
179763fae79SScott Long }
180763fae79SScott Long sb.st_size -= nread;
181763fae79SScott Long offset += nread;
182763fae79SScott Long }
183763fae79SScott Long
184763fae79SScott Long /* Kick off the flash. */
185763fae79SScott Long printf("WARNING: Firmware flash in progress, do not reboot machine... ");
186763fae79SScott Long fflush(stdout);
187*bac98f86SWHR if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
188*bac98f86SWHR NULL, 0, &status) < 0) {
189*bac98f86SWHR error = errno;
190*bac98f86SWHR printf("failed:\n\t%s\n", strerror(error));
191*bac98f86SWHR goto error;
192*bac98f86SWHR }
193763fae79SScott Long if (status != MFI_STAT_OK) {
194763fae79SScott Long printf("failed:\n\t%s\n", mfi_status(status));
195375c4656SBjoern A. Zeeb error = ENXIO;
196375c4656SBjoern A. Zeeb goto error;
197763fae79SScott Long }
198763fae79SScott Long printf("finished\n");
199c02999d9SJohn Baldwin error = display_pending_firmware(fd);
200763fae79SScott Long
201375c4656SBjoern A. Zeeb error:
202375c4656SBjoern A. Zeeb free(buf);
203375c4656SBjoern A. Zeeb if (fd >= 0)
204763fae79SScott Long close(fd);
205375c4656SBjoern A. Zeeb close(flash);
206763fae79SScott Long
207c02999d9SJohn Baldwin return (error);
208763fae79SScott Long }
209763fae79SScott Long MFI_COMMAND(top, flash, flash_adapter);
210