GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / greybus / Documentation / firmware / firmware.c
1 /*
2  * Sample code to test firmware-management protocol
3  *
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.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2016 Google Inc. All rights reserved.
10  * Copyright(c) 2016 Linaro Ltd. All rights reserved.
11  *
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.
15  *
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.
20  *
21  * BSD LICENSE
22  *
23  * Copyright(c) 2016 Google Inc. All rights reserved.
24  * Copyright(c) 2016 Linaro Ltd. All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  *
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
35  *    distribution.
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
39  *    permission.
40  *
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.
52  */
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <sys/ioctl.h>
59 #include <sys/stat.h>
60 #include <fcntl.h>
61
62 #include "../../greybus_firmware.h"
63
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
69
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;
74
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;
79
80 static void usage(void)
81 {
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");
83 }
84
85 static int update_intf_firmware(int fd)
86 {
87         int ret;
88
89         /* Get Interface Firmware Version */
90         printf("Get Interface Firmware Version\n");
91
92         ret = ioctl(fd, FW_MGMT_IOC_GET_INTF_FW, &intf_fw_info);
93         if (ret < 0) {
94                 printf("Failed to get interface firmware version: %s (%d)\n",
95                         fwdev, ret);
96                 return -1;
97         }
98
99         printf("Interface Firmware tag (%s), major (%d), minor (%d)\n",
100                 intf_fw_info.firmware_tag, intf_fw_info.major,
101                 intf_fw_info.minor);
102
103         /* Try Interface Firmware load over Unipro */
104         printf("Loading Interface Firmware\n");
105
106         intf_load.load_method = GB_FW_U_LOAD_METHOD_UNIPRO;
107         intf_load.status = 0;
108         intf_load.major = 0;
109         intf_load.minor = 0;
110
111         strncpy((char *)&intf_load.firmware_tag, firmware_tag,
112                 GB_FIRMWARE_U_TAG_MAX_SIZE);
113
114         ret = ioctl(fd, FW_MGMT_IOC_INTF_LOAD_AND_VALIDATE, &intf_load);
115         if (ret < 0) {
116                 printf("Failed to load interface firmware: %s (%d)\n", fwdev,
117                         ret);
118                 return -1;
119         }
120
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",
124                         intf_load.status);
125                 return -1;
126         }
127
128         printf("Interface Firmware (%s) Load done: major: %d, minor: %d, status: %d\n",
129                 firmware_tag, intf_load.major, intf_load.minor,
130                 intf_load.status);
131
132         /* Initiate Mode-switch to the newly loaded firmware */
133         printf("Initiate Mode switch\n");
134
135         ret = ioctl(fd, FW_MGMT_IOC_MODE_SWITCH);
136         if (ret < 0)
137                 printf("Failed to initiate mode-switch (%d)\n", ret);
138
139         return ret;
140 }
141
142 static int update_backend_firmware(int fd)
143 {
144         int ret;
145
146         /* Get Backend Firmware Version */
147         printf("Getting Backend Firmware Version\n");
148
149         strncpy((char *)&backend_fw_info.firmware_tag, firmware_tag,
150                 GB_FIRMWARE_U_TAG_MAX_SIZE);
151
152 retry_fw_version:
153         ret = ioctl(fd, FW_MGMT_IOC_GET_BACKEND_FW, &backend_fw_info);
154         if (ret < 0) {
155                 printf("Failed to get backend firmware version: %s (%d)\n",
156                         fwdev, ret);
157                 return -1;
158         }
159
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);
163
164         if (backend_fw_info.status == GB_FW_U_BACKEND_VERSION_STATUS_RETRY)
165                 goto retry_fw_version;
166
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);
171                 return -1;
172         }
173
174         /* Try Backend Firmware Update over Unipro */
175         printf("Updating Backend Firmware\n");
176
177         strncpy((char *)&backend_update.firmware_tag, firmware_tag,
178                 GB_FIRMWARE_U_TAG_MAX_SIZE);
179
180 retry_fw_update:
181         backend_update.status = 0;
182
183         ret = ioctl(fd, FW_MGMT_IOC_INTF_BACKEND_FW_UPDATE, &backend_update);
184         if (ret < 0) {
185                 printf("Failed to load backend firmware: %s (%d)\n", fwdev, ret);
186                 return -1;
187         }
188
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;
192         }
193
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);
197         } else {
198                 printf("Backend Firmware (%s) Load done: status: %d\n",
199                                 firmware_tag, backend_update.status);
200         }
201
202         return 0;
203 }
204
205 int main(int argc, char *argv[])
206 {
207         int fd, ret;
208         char *endptr;
209
210         if (argc > 1 &&
211             (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
212                 usage();
213                 return -1;
214         }
215
216         if (argc > 1)
217                 fwdev = argv[1];
218
219         if (argc > 2)
220                 fw_update_type = strtoul(argv[2], &endptr, 10);
221
222         if (argc > 3)
223                 firmware_tag = argv[3];
224         else if (!fw_update_type)
225                 firmware_tag = FW_TAG_INT_DEFAULT;
226         else
227                 firmware_tag = FW_TAG_BCND_DEFAULT;
228
229         if (argc > 4)
230                 fw_timeout = strtoul(argv[4], &endptr, 10);
231
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);
235
236         printf("Opening %s firmware management device\n", fwdev);
237
238         fd = open(fwdev, O_RDWR);
239         if (fd < 0) {
240                 printf("Failed to open: %s\n", fwdev);
241                 return -1;
242         }
243
244         /* Set Timeout */
245         printf("Setting timeout to %u ms\n", fw_timeout);
246
247         ret = ioctl(fd, FW_MGMT_IOC_SET_TIMEOUT_MS, &fw_timeout);
248         if (ret < 0) {
249                 printf("Failed to set timeout: %s (%d)\n", fwdev, ret);
250                 ret = -1;
251                 goto close_fd;
252         }
253
254         if (!fw_update_type)
255                 ret = update_intf_firmware(fd);
256         else
257                 ret = update_backend_firmware(fd);
258
259 close_fd:
260         close(fd);
261
262         return ret;
263 }