xref: /freebsd/usr.sbin/mfiutil/mfi_flash.c (revision c5c02a131a0e2ef52771e683269bc8778fe511f3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2008, 2009 Yahoo!, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The names of the authors may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
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 static int
46 display_pending_firmware(int fd)
47 {
48 	struct mfi_ctrl_info info;
49 	struct mfi_info_component header;
50 	int error;
51 	u_int i;
52 
53 	if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
54 		error = errno;
55 		warn("Failed to get controller info");
56 		return (error);
57 	}
58 
59 	printf("%s Pending Firmware Images:\n", mfi_device);
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], "");
72 
73 	return (0);
74 }
75 
76 static void
77 mbox_store_word(uint8_t *mbox, uint32_t val)
78 {
79 
80 	mbox[0] = val & 0xff;
81 	mbox[1] = val >> 8 & 0xff;
82 	mbox[2] = val >> 16 & 0xff;
83 	mbox[3] = val >> 24;
84 }
85 
86 static int
87 flash_adapter(int ac, char **av)
88 {
89 	struct mfi_progress dummy;
90 	off_t offset;
91 	size_t nread;
92 	char *buf;
93 	struct stat sb;
94 	int error, fd, flash;
95 	uint8_t mbox[4], status;
96 
97 	if (ac != 2) {
98 		warnx("flash: Firmware file required");
99 		return (EINVAL);
100 	}
101 
102 	flash = open(av[1], O_RDONLY);
103 	if (flash < 0) {
104 		error = errno;
105 		warn("flash: Failed to open %s", av[1]);
106 		return (error);
107 	}
108 
109 	buf = NULL;
110 	fd = -1;
111 
112 	if (fstat(flash, &sb) < 0) {
113 		error = errno;
114 		warn("fstat(%s)", av[1]);
115 		goto error;
116 	}
117 	if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) {
118 		warnx("Invalid flash file size");
119 		error = EINVAL;
120 		goto error;
121 	}
122 
123 	fd = mfi_open(mfi_device, O_RDWR);
124 	if (fd < 0) {
125 		error = errno;
126 		warn("mfi_open");
127 		goto error;
128 	}
129 
130 	/* First, ask the firmware to allocate space for the flash file. */
131 	mbox_store_word(mbox, sb.st_size);
132 	if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4,
133 	    &status) < 0) {
134 		error = errno;
135 		warn("Failed to allocate flash memory");
136 		goto error;
137 	}
138 	if (status != MFI_STAT_OK) {
139 		warnx("Failed to allocate flash memory: %s",
140 		    mfi_status(status));
141 		error = EIO;
142 		goto error;
143 	}
144 
145 	/* Upload the file 64k at a time. */
146 	buf = malloc(FLASH_BUF_SIZE);
147 	if (buf == NULL) {
148 		warnx("malloc failed");
149 		error = ENOMEM;
150 		goto error;
151 	}
152 	offset = 0;
153 	while (sb.st_size > 0) {
154 		nread = read(flash, buf, FLASH_BUF_SIZE);
155 		if (nread <= 0 || nread % 1024 != 0) {
156 			warnx("Bad read from flash file");
157 			if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE,
158 			    NULL, 0, NULL, 0, NULL) < 0) {
159 				warn("Failed to discard flash memory");
160 			}
161 			error = ENXIO;
162 			goto error;
163 		}
164 
165 		mbox_store_word(mbox, offset);
166 		if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
167 		    mbox, 4, &status) < 0) {
168 			error = errno;
169 			warn("Failed to download firmware");
170 			goto error;
171 		}
172 		if (status != MFI_STAT_OK) {
173 			warnx("Failed to download firmware: %s",
174 			    mfi_status(status));
175 			mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL,
176 			    0, NULL, 0, NULL);
177 			error = ENXIO;
178 			goto error;
179 		}
180 		sb.st_size -= nread;
181 		offset += nread;
182 	}
183 
184 	/* Kick off the flash. */
185 	printf("WARNING: Firmware flash in progress, do not reboot machine... ");
186 	fflush(stdout);
187 	if (mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
188 	    NULL, 0, &status) < 0) {
189 		error = errno;
190 		printf("failed:\n\t%s\n", strerror(error));
191 		goto error;
192 	}
193 	if (status != MFI_STAT_OK) {
194 		printf("failed:\n\t%s\n", mfi_status(status));
195 		error = ENXIO;
196 		goto error;
197 	}
198 	printf("finished\n");
199 	error = display_pending_firmware(fd);
200 
201 error:
202 	free(buf);
203 	if (fd >= 0)
204 		close(fd);
205 	close(flash);
206 
207 	return (error);
208 }
209 MFI_COMMAND(top, flash, flash_adapter);
210