xref: /freebsd/usr.sbin/mfiutil/mfi_flash.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1763fae79SScott Long /*-
2*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*1de7b4b8SPedro 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  * $FreeBSD$
32763fae79SScott Long  */
33763fae79SScott Long 
34763fae79SScott Long #include <sys/param.h>
35763fae79SScott Long #include <sys/errno.h>
36763fae79SScott Long #include <sys/stat.h>
37763fae79SScott Long #include <err.h>
38763fae79SScott Long #include <fcntl.h>
39763fae79SScott Long #include <stdio.h>
40763fae79SScott Long #include <stdlib.h>
41763fae79SScott Long #include <string.h>
42763fae79SScott Long #include <unistd.h>
43763fae79SScott Long #include "mfiutil.h"
44763fae79SScott Long 
45763fae79SScott Long #define	FLASH_BUF_SIZE	(64 * 1024)
46763fae79SScott Long 
47c02999d9SJohn Baldwin static int
48763fae79SScott Long display_pending_firmware(int fd)
49763fae79SScott Long {
50763fae79SScott Long 	struct mfi_ctrl_info info;
51763fae79SScott Long 	struct mfi_info_component header;
52c02999d9SJohn Baldwin 	int error;
53763fae79SScott Long 	u_int i;
54763fae79SScott Long 
55763fae79SScott Long 	if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
56c02999d9SJohn Baldwin 		error = errno;
57763fae79SScott Long 		warn("Failed to get controller info");
58c02999d9SJohn Baldwin 		return (error);
59763fae79SScott Long 	}
60763fae79SScott Long 
61763fae79SScott Long 	printf("mfi%d Pending Firmware Images:\n", mfi_unit);
62763fae79SScott Long 	strcpy(header.name, "Name");
63763fae79SScott Long 	strcpy(header.version, "Version");
64763fae79SScott Long 	strcpy(header.build_date, "Date");
65763fae79SScott Long 	strcpy(header.build_time, "Time");
66763fae79SScott Long 	scan_firmware(&header);
67763fae79SScott Long 	if (info.pending_image_component_count > 8)
68763fae79SScott Long 		info.pending_image_component_count = 8;
69763fae79SScott Long 	for (i = 0; i < info.pending_image_component_count; i++)
70763fae79SScott Long 		scan_firmware(&info.pending_image_component[i]);
71186475e2SEd Schouten 	display_firmware(&header, "");
72763fae79SScott Long 	for (i = 0; i < info.pending_image_component_count; i++)
73186475e2SEd Schouten 		display_firmware(&info.pending_image_component[i], "");
74c02999d9SJohn Baldwin 
75c02999d9SJohn Baldwin 	return (0);
76763fae79SScott Long }
77763fae79SScott Long 
78763fae79SScott Long static void
79763fae79SScott Long mbox_store_word(uint8_t *mbox, uint32_t val)
80763fae79SScott Long {
81763fae79SScott Long 
82763fae79SScott Long 	mbox[0] = val & 0xff;
83763fae79SScott Long 	mbox[1] = val >> 8 & 0xff;
84763fae79SScott Long 	mbox[2] = val >> 16 & 0xff;
85763fae79SScott Long 	mbox[3] = val >> 24;
86763fae79SScott Long }
87763fae79SScott Long 
88763fae79SScott Long static int
89763fae79SScott Long flash_adapter(int ac, char **av)
90763fae79SScott Long {
91763fae79SScott Long 	struct mfi_progress dummy;
92763fae79SScott Long 	off_t offset;
93763fae79SScott Long 	size_t nread;
94763fae79SScott Long 	char *buf;
95763fae79SScott Long 	struct stat sb;
96c02999d9SJohn Baldwin 	int error, fd, flash;
97763fae79SScott Long 	uint8_t mbox[4], status;
98763fae79SScott Long 
99763fae79SScott Long 	if (ac != 2) {
100763fae79SScott Long 		warnx("flash: Firmware file required");
101763fae79SScott Long 		return (EINVAL);
102763fae79SScott Long 	}
103763fae79SScott Long 
104763fae79SScott Long 	flash = open(av[1], O_RDONLY);
105763fae79SScott Long 	if (flash < 0) {
106c02999d9SJohn Baldwin 		error = errno;
107763fae79SScott Long 		warn("flash: Failed to open %s", av[1]);
108c02999d9SJohn Baldwin 		return (error);
109763fae79SScott Long 	}
110763fae79SScott Long 
111375c4656SBjoern A. Zeeb 	buf = NULL;
112375c4656SBjoern A. Zeeb 	fd = -1;
113375c4656SBjoern A. Zeeb 
114763fae79SScott Long 	if (fstat(flash, &sb) < 0) {
115c02999d9SJohn Baldwin 		error = errno;
116763fae79SScott Long 		warn("fstat(%s)", av[1]);
117375c4656SBjoern A. Zeeb 		goto error;
118763fae79SScott Long 	}
119763fae79SScott Long 	if (sb.st_size % 1024 != 0 || sb.st_size > 0x7fffffff) {
120763fae79SScott Long 		warnx("Invalid flash file size");
121375c4656SBjoern A. Zeeb 		error = EINVAL;
122375c4656SBjoern A. Zeeb 		goto error;
123763fae79SScott Long 	}
124763fae79SScott Long 
125bf4ec4dfSEitan Adler 	fd = mfi_open(mfi_unit, O_RDWR);
126763fae79SScott Long 	if (fd < 0) {
127c02999d9SJohn Baldwin 		error = errno;
128763fae79SScott Long 		warn("mfi_open");
129375c4656SBjoern A. Zeeb 		goto error;
130763fae79SScott Long 	}
131763fae79SScott Long 
132763fae79SScott Long 	/* First, ask the firmware to allocate space for the flash file. */
133763fae79SScott Long 	mbox_store_word(mbox, sb.st_size);
134763fae79SScott Long 	mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, &status);
135763fae79SScott Long 	if (status != MFI_STAT_OK) {
136763fae79SScott Long 		warnx("Failed to alloc flash memory: %s", mfi_status(status));
137375c4656SBjoern A. Zeeb 		error = EIO;
138375c4656SBjoern A. Zeeb 		goto error;
139763fae79SScott Long 	}
140763fae79SScott Long 
141763fae79SScott Long 	/* Upload the file 64k at a time. */
142763fae79SScott Long 	buf = malloc(FLASH_BUF_SIZE);
1433c22a809SJohn Baldwin 	if (buf == NULL) {
1443c22a809SJohn Baldwin 		warnx("malloc failed");
145375c4656SBjoern A. Zeeb 		error = ENOMEM;
146375c4656SBjoern A. Zeeb 		goto error;
1473c22a809SJohn Baldwin 	}
148763fae79SScott Long 	offset = 0;
149763fae79SScott Long 	while (sb.st_size > 0) {
150763fae79SScott Long 		nread = read(flash, buf, FLASH_BUF_SIZE);
151763fae79SScott Long 		if (nread <= 0 || nread % 1024 != 0) {
152763fae79SScott Long 			warnx("Bad read from flash file");
153763fae79SScott Long 			mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
154763fae79SScott Long 			    NULL, 0, NULL);
155375c4656SBjoern A. Zeeb 			error = ENXIO;
156375c4656SBjoern A. Zeeb 			goto error;
157763fae79SScott Long 		}
158763fae79SScott Long 
159763fae79SScott Long 		mbox_store_word(mbox, offset);
160763fae79SScott Long 		mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
161763fae79SScott Long 		    mbox, 4, &status);
162763fae79SScott Long 		if (status != MFI_STAT_OK) {
163763fae79SScott Long 			warnx("Flash download failed: %s", mfi_status(status));
164763fae79SScott Long 			mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
165763fae79SScott Long 			    NULL, 0, NULL);
166375c4656SBjoern A. Zeeb 			error = ENXIO;
167375c4656SBjoern A. Zeeb 			goto error;
168763fae79SScott Long 		}
169763fae79SScott Long 		sb.st_size -= nread;
170763fae79SScott Long 		offset += nread;
171763fae79SScott Long 	}
172763fae79SScott Long 
173763fae79SScott Long 	/* Kick off the flash. */
174763fae79SScott Long 	printf("WARNING: Firmware flash in progress, do not reboot machine... ");
175763fae79SScott Long 	fflush(stdout);
176763fae79SScott Long 	mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
177763fae79SScott Long 	    NULL, 0, &status);
178763fae79SScott Long 	if (status != MFI_STAT_OK) {
179763fae79SScott Long 		printf("failed:\n\t%s\n", mfi_status(status));
180375c4656SBjoern A. Zeeb 		error = ENXIO;
181375c4656SBjoern A. Zeeb 		goto error;
182763fae79SScott Long 	}
183763fae79SScott Long 	printf("finished\n");
184c02999d9SJohn Baldwin 	error = display_pending_firmware(fd);
185763fae79SScott Long 
186375c4656SBjoern A. Zeeb error:
187375c4656SBjoern A. Zeeb 	free(buf);
188375c4656SBjoern A. Zeeb 	if (fd >= 0)
189763fae79SScott Long 		close(fd);
190375c4656SBjoern A. Zeeb 	close(flash);
191763fae79SScott Long 
192c02999d9SJohn Baldwin 	return (error);
193763fae79SScott Long }
194763fae79SScott Long MFI_COMMAND(top, flash, flash_adapter);
195