2 * Sample code to test firmware-management protocol
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * Copyright(c) 2016 Google Inc. All rights reserved.
10 * Copyright(c) 2016 Linaro Ltd. All rights reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License version 2 for more details.
23 * Copyright(c) 2016 Google Inc. All rights reserved.
24 * Copyright(c) 2016 Linaro Ltd. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
30 * * Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * * Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in
34 * the documentation and/or other materials provided with the
36 * * Neither the name of Google Inc. or Linaro Ltd. nor the names of
37 * its contributors may be used to endorse or promote products
38 * derived from this software without specific prior written
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR
45 * LINARO LTD. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
46 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
48 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
49 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/ioctl.h>
62 #include "../../greybus_firmware.h"
64 #define FW_DEV_DEFAULT "/dev/gb-fw-mgmt-0"
65 #define FW_TAG_INT_DEFAULT "s3f"
66 #define FW_TAG_BCND_DEFAULT "bf_01"
67 #define FW_UPDATE_TYPE_DEFAULT 0
68 #define FW_TIMEOUT_DEFAULT 10000
70 static const char *firmware_tag;
71 static const char *fwdev = FW_DEV_DEFAULT;
72 static unsigned int fw_update_type = FW_UPDATE_TYPE_DEFAULT;
73 static unsigned int fw_timeout = FW_TIMEOUT_DEFAULT;
75 static struct fw_mgmt_ioc_get_intf_version intf_fw_info;
76 static struct fw_mgmt_ioc_get_backend_version backend_fw_info;
77 static struct fw_mgmt_ioc_intf_load_and_validate intf_load;
78 static struct fw_mgmt_ioc_backend_fw_update backend_update;
80 static void usage(void)
82 printf("\nUsage: ./firmware <gb-fw-mgmt-X (default: gb-fw-mgmt-0)> <interface: 0, backend: 1 (default: 0)> <firmware-tag> (default: \"s3f\"/\"bf_01\") <timeout (default: 10000 ms)>\n");
85 static int update_intf_firmware(int fd)
89 /* Get Interface Firmware Version */
90 printf("Get Interface Firmware Version\n");
92 ret = ioctl(fd, FW_MGMT_IOC_GET_INTF_FW, &intf_fw_info);
94 printf("Failed to get interface firmware version: %s (%d)\n",
99 printf("Interface Firmware tag (%s), major (%d), minor (%d)\n",
100 intf_fw_info.firmware_tag, intf_fw_info.major,
103 /* Try Interface Firmware load over Unipro */
104 printf("Loading Interface Firmware\n");
106 intf_load.load_method = GB_FW_U_LOAD_METHOD_UNIPRO;
107 intf_load.status = 0;
111 strncpy((char *)&intf_load.firmware_tag, firmware_tag,
112 GB_FIRMWARE_U_TAG_MAX_SIZE);
114 ret = ioctl(fd, FW_MGMT_IOC_INTF_LOAD_AND_VALIDATE, &intf_load);
116 printf("Failed to load interface firmware: %s (%d)\n", fwdev,
121 if (intf_load.status != GB_FW_U_LOAD_STATUS_VALIDATED &&
122 intf_load.status != GB_FW_U_LOAD_STATUS_UNVALIDATED) {
123 printf("Load status says loading failed: %d\n",
128 printf("Interface Firmware (%s) Load done: major: %d, minor: %d, status: %d\n",
129 firmware_tag, intf_load.major, intf_load.minor,
132 /* Initiate Mode-switch to the newly loaded firmware */
133 printf("Initiate Mode switch\n");
135 ret = ioctl(fd, FW_MGMT_IOC_MODE_SWITCH);
137 printf("Failed to initiate mode-switch (%d)\n", ret);
142 static int update_backend_firmware(int fd)
146 /* Get Backend Firmware Version */
147 printf("Getting Backend Firmware Version\n");
149 strncpy((char *)&backend_fw_info.firmware_tag, firmware_tag,
150 GB_FIRMWARE_U_TAG_MAX_SIZE);
153 ret = ioctl(fd, FW_MGMT_IOC_GET_BACKEND_FW, &backend_fw_info);
155 printf("Failed to get backend firmware version: %s (%d)\n",
160 printf("Backend Firmware tag (%s), major (%d), minor (%d), status (%d)\n",
161 backend_fw_info.firmware_tag, backend_fw_info.major,
162 backend_fw_info.minor, backend_fw_info.status);
164 if (backend_fw_info.status == GB_FW_U_BACKEND_VERSION_STATUS_RETRY)
165 goto retry_fw_version;
167 if ((backend_fw_info.status != GB_FW_U_BACKEND_VERSION_STATUS_SUCCESS)
168 && (backend_fw_info.status != GB_FW_U_BACKEND_VERSION_STATUS_NOT_AVAILABLE)) {
169 printf("Failed to get backend firmware version: %s (%d)\n",
170 fwdev, backend_fw_info.status);
174 /* Try Backend Firmware Update over Unipro */
175 printf("Updating Backend Firmware\n");
177 strncpy((char *)&backend_update.firmware_tag, firmware_tag,
178 GB_FIRMWARE_U_TAG_MAX_SIZE);
181 backend_update.status = 0;
183 ret = ioctl(fd, FW_MGMT_IOC_INTF_BACKEND_FW_UPDATE, &backend_update);
185 printf("Failed to load backend firmware: %s (%d)\n", fwdev, ret);
189 if (backend_update.status == GB_FW_U_BACKEND_FW_STATUS_RETRY) {
190 printf("Retrying firmware update: %d\n", backend_update.status);
191 goto retry_fw_update;
194 if (backend_update.status != GB_FW_U_BACKEND_FW_STATUS_SUCCESS) {
195 printf("Load status says loading failed: %d\n",
196 backend_update.status);
198 printf("Backend Firmware (%s) Load done: status: %d\n",
199 firmware_tag, backend_update.status);
205 int main(int argc, char *argv[])
211 (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
220 fw_update_type = strtoul(argv[2], &endptr, 10);
223 firmware_tag = argv[3];
224 else if (!fw_update_type)
225 firmware_tag = FW_TAG_INT_DEFAULT;
227 firmware_tag = FW_TAG_BCND_DEFAULT;
230 fw_timeout = strtoul(argv[4], &endptr, 10);
232 printf("Trying Firmware update: fwdev: %s, type: %s, tag: %s, timeout: %u\n",
233 fwdev, fw_update_type == 0 ? "interface" : "backend",
234 firmware_tag, fw_timeout);
236 printf("Opening %s firmware management device\n", fwdev);
238 fd = open(fwdev, O_RDWR);
240 printf("Failed to open: %s\n", fwdev);
245 printf("Setting timeout to %u ms\n", fw_timeout);
247 ret = ioctl(fd, FW_MGMT_IOC_SET_TIMEOUT_MS, &fw_timeout);
249 printf("Failed to set timeout: %s (%d)\n", fwdev, ret);
255 ret = update_intf_firmware(fd);
257 ret = update_backend_firmware(fd);