2 * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
28 #include <sys/param.h>
30 #include <sys/endian.h>
43 MPS_TABLE(top, flash);
46 flash_save(int argc, char **argv)
48 const char *firmware_file;
49 unsigned char *firmware_buffer = NULL;
52 ssize_t written = 0, ret = 0;
55 warnx("missing argument: expecting 'firmware' or bios'");
59 if (strcmp(argv[1], "bios") == 0) {
61 } else if (strcmp(argv[1], "firmware") != 0) {
62 warnx("Invalid argument '%s', expecting 'firmware' or 'bios'",
67 warnx("save %s: extra arguments", argv[1]);
71 firmware_file = argv[1];
73 firmware_file = argv[2];
76 fd = mps_open(mps_unit);
83 if ((size = mps_firmware_get(fd, &firmware_buffer, bios)) < 0) {
84 warnx("Fail to save %s", argv[1]);
91 fd = open(firmware_file, O_CREAT | O_TRUNC | O_RDWR, 0644);
95 free(firmware_buffer);
98 while (written != size) {
99 if ((ret = write(fd, firmware_buffer + written, size - written)) <0) {
102 free(firmware_buffer);
110 free(firmware_buffer);
111 printf("%s successfully saved as %s\n", argv[1], firmware_file);
115 MPS_COMMAND(flash, save, flash_save, "[firmware|bios] [file]",
116 "Save firmware/bios into a file");
119 flash_update(int argc, char **argv)
122 unsigned char *mem = NULL;
125 MPI2_FW_IMAGE_HEADER *fwheader;
126 MPI2_IOC_FACTS_REPLY *facts;
129 warnx("missing argument: expecting 'firmware' or bios'");
133 if (strcmp(argv[1], "bios") == 0) {
135 } else if (strcmp(argv[1], "firmware") != 0) {
136 warnx("Invalid argument '%s', expecting 'firmware' or 'bios'",
141 warnx("update firmware: extra arguments");
146 warnx("no firmware specified");
150 if (stat(argv[2], &st) == -1) {
156 fd = open(argv[2], O_RDONLY);
163 mem = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
164 if (mem == MAP_FAILED) {
172 fd = mps_open(mps_unit);
176 munmap(mem, st.st_size);
180 if ((facts = mps_get_iocfacts(fd)) == NULL) {
181 warnx("could not get controller IOCFacts\n");
182 munmap(mem, st.st_size);
188 /* Check boot record magic number */
189 if (((mem[0x01]<<8) + mem[0x00]) != 0xaa55) {
190 warnx("Invalid bios: no boot record magic number");
191 munmap(mem, st.st_size);
196 if ((st.st_size % 512) != 0) {
197 warnx("Invalid bios: size not a multiple of 512");
198 munmap(mem, st.st_size);
204 fwheader = (MPI2_FW_IMAGE_HEADER *)mem;
205 if (le16toh(fwheader->VendorID) != MPI2_MFGPAGE_VENDORID_LSI) {
206 warnx("Invalid firmware:");
207 warnx(" Expected Vendor ID: %04x",
208 MPI2_MFGPAGE_VENDORID_LSI);
209 warnx(" Image Vendor ID: %04x", le16toh(fwheader->VendorID));
210 munmap(mem, st.st_size);
216 if (le16toh(fwheader->ProductID) != facts->ProductID) {
217 warnx("Invalid image:");
218 warnx(" Expected Product ID: %04x", facts->ProductID);
219 warnx(" Image Product ID: %04x", le16toh(fwheader->ProductID));
220 munmap(mem, st.st_size);
227 printf("Updating %s...\n", argv[1]);
228 if (mps_firmware_send(fd, mem, st.st_size, bios) < 0) {
229 warnx("Fail to update %s", argv[1]);
230 munmap(mem, st.st_size);
236 munmap(mem, st.st_size);
239 printf("%s successfully updated\n", argv[1]);
243 MPS_COMMAND(flash, update, flash_update, "[firmware|bios] file",
244 "Update firmware/bios");