xref: /freebsd/usr.sbin/mfiutil/mfi_flash.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
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$
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("mfi%d Pending Firmware Images:\n", mfi_unit);
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_unit, 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 	mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_OPEN, NULL, 0, mbox, 4, &status);
133 	if (status != MFI_STAT_OK) {
134 		warnx("Failed to alloc flash memory: %s", mfi_status(status));
135 		error = EIO;
136 		goto error;
137 	}
138 
139 	/* Upload the file 64k at a time. */
140 	buf = malloc(FLASH_BUF_SIZE);
141 	if (buf == NULL) {
142 		warnx("malloc failed");
143 		error = ENOMEM;
144 		goto error;
145 	}
146 	offset = 0;
147 	while (sb.st_size > 0) {
148 		nread = read(flash, buf, FLASH_BUF_SIZE);
149 		if (nread <= 0 || nread % 1024 != 0) {
150 			warnx("Bad read from flash file");
151 			mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
152 			    NULL, 0, NULL);
153 			error = ENXIO;
154 			goto error;
155 		}
156 
157 		mbox_store_word(mbox, offset);
158 		mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_DOWNLOAD, buf, nread,
159 		    mbox, 4, &status);
160 		if (status != MFI_STAT_OK) {
161 			warnx("Flash download failed: %s", mfi_status(status));
162 			mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_CLOSE, NULL, 0,
163 			    NULL, 0, NULL);
164 			error = ENXIO;
165 			goto error;
166 		}
167 		sb.st_size -= nread;
168 		offset += nread;
169 	}
170 
171 	/* Kick off the flash. */
172 	printf("WARNING: Firmware flash in progress, do not reboot machine... ");
173 	fflush(stdout);
174 	mfi_dcmd_command(fd, MFI_DCMD_FLASH_FW_FLASH, &dummy, sizeof(dummy),
175 	    NULL, 0, &status);
176 	if (status != MFI_STAT_OK) {
177 		printf("failed:\n\t%s\n", mfi_status(status));
178 		error = ENXIO;
179 		goto error;
180 	}
181 	printf("finished\n");
182 	error = display_pending_firmware(fd);
183 
184 error:
185 	free(buf);
186 	if (fd >= 0)
187 		close(fd);
188 	close(flash);
189 
190 	return (error);
191 }
192 MFI_COMMAND(top, flash, flash_adapter);
193