| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* qmi-firmware-update -- Command line tool to update firmware in QMI devices |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* Copyright (C) 2019 Zodiac Inflight Innovations |
| 19 |
* Copyright (C) 2019 Aleksander Morgado <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <glib-object.h> |
| 23 |
#include <gio/gio.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "qfu-image.h" |
| 27 |
|
| 28 |
#include "qfu-sahara-message.h" |
| 29 |
#include "qfu-enum-types.h" |
| 30 |
|
| 31 |
/******************************************************************************/ |
| 32 |
/* Sahara messages */ |
| 33 |
|
| 34 |
#define CURRENT_SAHARA_VERSION 0x00000002 |
| 35 |
|
| 36 |
typedef struct _SaharaHelloRequest SaharaHelloRequest; |
| 37 |
struct _SaharaHelloRequest { |
| 38 |
QfuSaharaHeader header; |
| 39 |
guint32 version; |
| 40 |
guint32 compatible; |
| 41 |
guint32 max_len; |
| 42 |
guint32 mode; |
| 43 |
guint32 reserved[6]; |
| 44 |
} __attribute__ ((packed)); |
| 45 |
|
| 46 |
gboolean |
| 47 |
qfu_sahara_request_hello_parse (const guint8 *buffer, |
| 48 |
gsize buffer_len, |
| 49 |
GError **error) |
| 50 |
{ |
| 51 |
SaharaHelloRequest *msg; |
| 52 |
QfuSaharaMode mode; |
| 53 |
const gchar *mode_str; |
| 54 |
|
| 55 |
if (buffer_len < sizeof (SaharaHelloRequest)) { |
| 56 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 57 |
"message size mismatch: %" G_GSIZE_FORMAT " != %" G_GSIZE_FORMAT, |
| 58 |
buffer_len, sizeof (SaharaHelloRequest)); |
| 59 |
return FALSE; |
| 60 |
} |
| 61 |
|
| 62 |
msg = (SaharaHelloRequest *) buffer; |
| 63 |
g_assert (msg->header.cmd == GUINT32_FROM_LE (QFU_SAHARA_CMD_HELLO_REQ)); |
| 64 |
mode = (QfuSaharaMode) GUINT32_FROM_LE (msg->mode); |
| 65 |
mode_str = qfu_sahara_mode_get_string (mode); |
| 66 |
|
| 67 |
g_debug ("[qfu,sahara-message] received %s:", qfu_sahara_cmd_get_string (QFU_SAHARA_CMD_HELLO_REQ)); |
| 68 |
g_debug ("[qfu,sahara-message] version: %u", GUINT32_FROM_LE (msg->version)); |
| 69 |
g_debug ("[qfu,sahara-message] compatible: %u", GUINT32_FROM_LE (msg->compatible)); |
| 70 |
g_debug ("[qfu,sahara-message] max length: %u", GUINT32_FROM_LE (msg->max_len)); |
| 71 |
if (mode_str) |
| 72 |
g_debug ("[qfu,sahara-message] mode: %s", mode_str ? mode_str : "unknown"); |
| 73 |
else |
| 74 |
g_debug ("[qfu,sahara-message] mode: unknown (0x%08x)", mode); |
| 75 |
|
| 76 |
/* our version needs to be greater or equal than the minimum version reported */ |
| 77 |
if (GUINT32_FROM_LE (msg->compatible) > CURRENT_SAHARA_VERSION) { |
| 78 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 79 |
"unsupported sahara version (%u > %u)", |
| 80 |
GUINT32_FROM_LE (msg->compatible), CURRENT_SAHARA_VERSION); |
| 81 |
return FALSE; |
| 82 |
} |
| 83 |
|
| 84 |
return TRUE; |
| 85 |
} |
| 86 |
|
| 87 |
typedef struct _SaharaHelloResponse SaharaHelloResponse; |
| 88 |
struct _SaharaHelloResponse { |
| 89 |
QfuSaharaHeader header; |
| 90 |
guint32 version; |
| 91 |
guint32 compatible; |
| 92 |
guint32 status; |
| 93 |
guint32 mode; |
| 94 |
guint32 reserved[6]; |
| 95 |
} __attribute__ ((packed)); |
| 96 |
|
| 97 |
gsize |
| 98 |
qfu_sahara_response_hello_build (guint8 *buffer, |
| 99 |
gsize buffer_len) |
| 100 |
{ |
| 101 |
SaharaHelloResponse *msg = (SaharaHelloResponse *)buffer; |
| 102 |
|
| 103 |
g_assert (buffer_len >= sizeof (SaharaHelloResponse)); |
| 104 |
|
| 105 |
msg->header.cmd = GUINT32_TO_LE (QFU_SAHARA_CMD_HELLO_RSP); |
| 106 |
msg->header.size = GUINT32_TO_LE (sizeof (SaharaHelloResponse)); |
| 107 |
msg->version = GUINT32_TO_LE (CURRENT_SAHARA_VERSION); |
| 108 |
msg->compatible = GUINT32_TO_LE (CURRENT_SAHARA_VERSION); |
| 109 |
msg->status = GUINT32_TO_LE (QFU_SAHARA_STATUS_SUCCESS); |
| 110 |
msg->mode = GUINT32_TO_LE (QFU_SAHARA_MODE_COMMAND); |
| 111 |
memset (msg->reserved, 0, sizeof (msg->reserved)); |
| 112 |
|
| 113 |
return sizeof (SaharaHelloResponse); |
| 114 |
} |
| 115 |
|
| 116 |
#define EXECUTE_SWITCH_FIREHOSE 0x0000ff00 |
| 117 |
|
| 118 |
typedef struct _SaharaCommandExecuteRequest SaharaCommandExecuteRequest; |
| 119 |
struct _SaharaCommandExecuteRequest { |
| 120 |
QfuSaharaHeader header; |
| 121 |
guint32 execute; |
| 122 |
} __attribute__ ((packed)); |
| 123 |
|
| 124 |
gsize |
| 125 |
qfu_sahara_request_switch_build (guint8 *buffer, |
| 126 |
gsize buffer_len) |
| 127 |
{ |
| 128 |
SaharaCommandExecuteRequest *msg = (SaharaCommandExecuteRequest *)buffer; |
| 129 |
|
| 130 |
g_assert (buffer_len >= sizeof (SaharaCommandExecuteRequest)); |
| 131 |
|
| 132 |
msg->header.cmd = GUINT32_TO_LE (QFU_SAHARA_CMD_COMMAND_EXECUTE_REQ); |
| 133 |
msg->header.size = GUINT32_TO_LE (sizeof (SaharaCommandExecuteRequest)); |
| 134 |
msg->execute = GUINT32_TO_LE (EXECUTE_SWITCH_FIREHOSE); |
| 135 |
|
| 136 |
return sizeof (SaharaCommandExecuteRequest); |
| 137 |
} |
| 138 |
|
| 139 |
typedef struct _SaharaCommandExecuteResponse SaharaCommandExecuteResponse; |
| 140 |
struct _SaharaCommandExecuteResponse { |
| 141 |
QfuSaharaHeader header; |
| 142 |
guint32 execute; |
| 143 |
guint32 expected_data_length; |
| 144 |
} __attribute__ ((packed)); |
| 145 |
|
| 146 |
gboolean |
| 147 |
qfu_sahara_response_switch_parse (const guint8 *buffer, |
| 148 |
gsize buffer_len, |
| 149 |
GError **error) |
| 150 |
{ |
| 151 |
SaharaCommandExecuteResponse *msg; |
| 152 |
|
| 153 |
if (buffer_len < sizeof (SaharaCommandExecuteResponse)) { |
| 154 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 155 |
"message size mismatch: %" G_GSIZE_FORMAT " != %" G_GSIZE_FORMAT, |
| 156 |
buffer_len, sizeof (SaharaCommandExecuteResponse)); |
| 157 |
return FALSE; |
| 158 |
} |
| 159 |
|
| 160 |
msg = (SaharaCommandExecuteResponse *) buffer; |
| 161 |
g_assert (msg->header.cmd == GUINT32_FROM_LE (QFU_SAHARA_CMD_COMMAND_EXECUTE_RSP)); |
| 162 |
|
| 163 |
g_debug ("[qfu,sahara-message] received %s:", qfu_sahara_cmd_get_string (QFU_SAHARA_CMD_COMMAND_EXECUTE_RSP)); |
| 164 |
g_debug ("[qfu,sahara-message] execute: 0x%08x", GUINT32_FROM_LE (msg->execute)); |
| 165 |
g_debug ("[qfu,sahara-message] expected data length: %u", GUINT32_FROM_LE (msg->expected_data_length)); |
| 166 |
|
| 167 |
/* Note: the expected data length is the length of the data expected in the next sahara |
| 168 |
* protocol step, the modem is telling us how much data it's going to send; e.g. the EM7565 |
| 169 |
* returns just 9 bytes ("confirmed"). Not doing anything else with this value because |
| 170 |
* we don't really need it for anything */ |
| 171 |
|
| 172 |
return TRUE; |
| 173 |
} |
| 174 |
|
| 175 |
typedef struct _SaharaCommandExecuteDataRequest SaharaCommandExecuteDataRequest; |
| 176 |
struct _SaharaCommandExecuteDataRequest { |
| 177 |
QfuSaharaHeader header; |
| 178 |
guint32 execute; |
| 179 |
} __attribute__ ((packed)); |
| 180 |
|
| 181 |
gsize |
| 182 |
qfu_sahara_request_switch_data_build (guint8 *buffer, |
| 183 |
gsize buffer_len) |
| 184 |
{ |
| 185 |
SaharaCommandExecuteDataRequest *msg = (SaharaCommandExecuteDataRequest *)buffer; |
| 186 |
|
| 187 |
g_assert (buffer_len >= sizeof (SaharaCommandExecuteDataRequest)); |
| 188 |
|
| 189 |
msg->header.cmd = GUINT32_TO_LE (QFU_SAHARA_CMD_COMMAND_EXECUTE_DATA); |
| 190 |
msg->header.size = GUINT32_TO_LE (sizeof (SaharaCommandExecuteDataRequest)); |
| 191 |
msg->execute = GUINT32_TO_LE (EXECUTE_SWITCH_FIREHOSE); |
| 192 |
|
| 193 |
return sizeof (SaharaCommandExecuteDataRequest); |
| 194 |
} |
| 195 |
|
| 196 |
typedef struct _SaharaEndImageTransferResponse SaharaEndImageTransferResponse; |
| 197 |
struct _SaharaEndImageTransferResponse { |
| 198 |
QfuSaharaHeader header; |
| 199 |
guint32 file; |
| 200 |
guint32 status; |
| 201 |
} __attribute__ ((packed)); |
| 202 |
|
| 203 |
gboolean |
| 204 |
qfu_sahara_response_end_image_transfer_parse (const guint8 *buffer, |
| 205 |
gsize buffer_len, |
| 206 |
GError **error) |
| 207 |
{ |
| 208 |
SaharaEndImageTransferResponse *msg; |
| 209 |
QfuSaharaStatus status; |
| 210 |
const gchar *status_str; |
| 211 |
|
| 212 |
if (buffer_len < sizeof (SaharaEndImageTransferResponse)) { |
| 213 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 214 |
"message size mismatch: %" G_GSIZE_FORMAT " != %" G_GSIZE_FORMAT, |
| 215 |
buffer_len, sizeof (SaharaEndImageTransferResponse)); |
| 216 |
return FALSE; |
| 217 |
} |
| 218 |
|
| 219 |
msg = (SaharaEndImageTransferResponse *) buffer; |
| 220 |
g_assert (msg->header.cmd == GUINT32_FROM_LE (QFU_SAHARA_CMD_COMMAND_END_IMAGE_TRANSFER)); |
| 221 |
status = (QfuSaharaStatus) GUINT32_FROM_LE (msg->status); |
| 222 |
status_str = qfu_sahara_status_get_string (status); |
| 223 |
|
| 224 |
g_debug ("[qfu,sahara-message] received %s:", qfu_sahara_cmd_get_string (QFU_SAHARA_CMD_COMMAND_END_IMAGE_TRANSFER)); |
| 225 |
g_debug ("[qfu,sahara-message] file: %u", GUINT32_FROM_LE (msg->file)); |
| 226 |
if (status_str) |
| 227 |
g_debug ("[qfu,sahara-message] status: %s", status_str); |
| 228 |
else |
| 229 |
g_debug ("[qfu,sahara-message] status: unknown (0x%08x)", status); |
| 230 |
|
| 231 |
if (status != QFU_SAHARA_STATUS_SUCCESS) { |
| 232 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 233 |
"operation failed: %s", status_str); |
| 234 |
return FALSE; |
| 235 |
} |
| 236 |
|
| 237 |
return TRUE; |
| 238 |
} |