| 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.h> |
| 23 |
#include <string.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include "qfu-firehose-message.h" |
| 26 |
|
| 27 |
/* NOTE: these parsers are NOT supposed to be general purpose parsers for the |
| 28 |
* firehose protocol message types, we're exclusively processing the messages |
| 29 |
* expected in the Sierra 9x50 firmware upgrade protocol, and therefore it's |
| 30 |
* assumed that using the regex matching approach is enough... */ |
| 31 |
|
| 32 |
#define FIREHOSE_MESSAGE_HEADER "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<data>\n" |
| 33 |
#define FIREHOSE_MESSAGE_TRAILER "\n</data>\n\n" |
| 34 |
|
| 35 |
gsize |
| 36 |
qfu_firehose_message_build_ping (guint8 *buffer, |
| 37 |
gsize buffer_len) |
| 38 |
{ |
| 39 |
g_snprintf ((gchar *)buffer, buffer_len, |
| 40 |
"%s" |
| 41 |
"<NOP value=\"ping\" />" |
| 42 |
"%s", |
| 43 |
FIREHOSE_MESSAGE_HEADER, |
| 44 |
FIREHOSE_MESSAGE_TRAILER); |
| 45 |
return strlen ((gchar *)buffer); |
| 46 |
} |
| 47 |
|
| 48 |
gsize |
| 49 |
qfu_firehose_message_build_configure (guint8 *buffer, |
| 50 |
gsize buffer_len, |
| 51 |
guint max_payload_size_to_target_in_bytes) |
| 52 |
{ |
| 53 |
|
| 54 |
|
| 55 |
g_snprintf ((gchar *)buffer, buffer_len, |
| 56 |
"%s" |
| 57 |
"<configure MemoryName=\"eMMC\" Verbose=\"0\" AlwaysValidate=\"0\" MaxDigestTableSizeInBytes=\"8192\" MaxPayloadSizeToTargetInBytes=\"%u\" ZlpAwareHost=\"0\" SkipStorageInit=\"0\" TargetName=\"8960\" />" |
| 58 |
"%s", |
| 59 |
FIREHOSE_MESSAGE_HEADER, |
| 60 |
/* if not a specific value given, pass some placeholder big value; we expect the modem |
| 61 |
* to return a NAK with the correct value to use after that. */ |
| 62 |
max_payload_size_to_target_in_bytes ? max_payload_size_to_target_in_bytes : 1048576, |
| 63 |
FIREHOSE_MESSAGE_TRAILER); |
| 64 |
return strlen ((gchar *)buffer); |
| 65 |
} |
| 66 |
|
| 67 |
gsize |
| 68 |
qfu_firehose_message_build_get_storage_info (guint8 *buffer, |
| 69 |
gsize buffer_len) |
| 70 |
{ |
| 71 |
g_snprintf ((gchar *)buffer, buffer_len, |
| 72 |
"%s" |
| 73 |
"<getStorageInfo physical_partition_number=\"0\" />" |
| 74 |
"%s", |
| 75 |
FIREHOSE_MESSAGE_HEADER, |
| 76 |
FIREHOSE_MESSAGE_TRAILER); |
| 77 |
return strlen ((gchar *)buffer); |
| 78 |
} |
| 79 |
|
| 80 |
gsize |
| 81 |
qfu_firehose_message_build_program (guint8 *buffer, |
| 82 |
gsize buffer_len, |
| 83 |
guint pages_per_block, |
| 84 |
guint sector_size_in_bytes, |
| 85 |
guint num_partition_sectors) |
| 86 |
{ |
| 87 |
g_snprintf ((gchar *)buffer, buffer_len, |
| 88 |
"%s" |
| 89 |
"<program PAGES_PER_BLOCK=\"%u\" SECTOR_SIZE_IN_BYTES=\"%u\" filename=\"spkg.cwe\" num_partition_sectors=\"%u\" physical_partition_number=\"0\" start_sector=\"-1\" />" |
| 90 |
"%s", |
| 91 |
FIREHOSE_MESSAGE_HEADER, |
| 92 |
pages_per_block, |
| 93 |
sector_size_in_bytes, |
| 94 |
num_partition_sectors, |
| 95 |
FIREHOSE_MESSAGE_TRAILER); |
| 96 |
return strlen ((gchar *)buffer); |
| 97 |
} |
| 98 |
|
| 99 |
gsize |
| 100 |
qfu_firehose_message_build_reset (guint8 *buffer, |
| 101 |
gsize buffer_len) |
| 102 |
{ |
| 103 |
g_snprintf ((gchar *)buffer, buffer_len, |
| 104 |
"%s" |
| 105 |
"<power DelayInSeconds=\"0\" value=\"reset\" />" |
| 106 |
"%s", |
| 107 |
FIREHOSE_MESSAGE_HEADER, |
| 108 |
FIREHOSE_MESSAGE_TRAILER); |
| 109 |
return strlen ((gchar *)buffer); |
| 110 |
} |
| 111 |
|
| 112 |
gboolean |
| 113 |
qfu_firehose_message_parse_response_ack (const gchar *rsp, |
| 114 |
gchar **value, |
| 115 |
gchar **rawmode) |
| 116 |
{ |
| 117 |
GRegex *r; |
| 118 |
GMatchInfo *match_info = NULL; |
| 119 |
gboolean success = FALSE; |
| 120 |
|
| 121 |
/* |
| 122 |
* <?xml version="1.0" encoding="UTF-8" ?> |
| 123 |
* <data> |
| 124 |
* <response value="ACK" rawmode="true" /> |
| 125 |
* </data> |
| 126 |
*/ |
| 127 |
|
| 128 |
r = g_regex_new ("<data>\\s*<response\\s*value=\"([^\"]*)\"(?:\\s*rawmode=\"([^\"]*)\")?\\s*/>\\s*</data>", |
| 129 |
G_REGEX_RAW, 0, NULL); |
| 130 |
g_assert (r); |
| 131 |
|
| 132 |
if (!g_regex_match (r, rsp, 0, &match_info)) |
| 133 |
goto out; |
| 134 |
|
| 135 |
if (!g_match_info_matches (match_info)) |
| 136 |
goto out; |
| 137 |
|
| 138 |
if (value) |
| 139 |
*value = g_match_info_fetch (match_info, 1); |
| 140 |
if (rawmode) { |
| 141 |
if (g_match_info_get_match_count (match_info) > 2) |
| 142 |
*rawmode = g_match_info_fetch (match_info, 2); |
| 143 |
else |
| 144 |
*rawmode = NULL; |
| 145 |
} |
| 146 |
|
| 147 |
success = TRUE; |
| 148 |
|
| 149 |
out: |
| 150 |
if (match_info) |
| 151 |
g_match_info_unref (match_info); |
| 152 |
g_regex_unref (r); |
| 153 |
return success; |
| 154 |
} |
| 155 |
|
| 156 |
gboolean |
| 157 |
qfu_firehose_message_parse_log (const gchar *rsp, |
| 158 |
gchar **value) |
| 159 |
{ |
| 160 |
GRegex *r; |
| 161 |
GMatchInfo *match_info = NULL; |
| 162 |
gboolean success = FALSE; |
| 163 |
|
| 164 |
/* |
| 165 |
* <?xml version="1.0" encoding="UTF-8" ?> |
| 166 |
* <data> |
| 167 |
* <log value="SWI supported functions: CWE"/> |
| 168 |
* </data> |
| 169 |
*/ |
| 170 |
|
| 171 |
r = g_regex_new ("<data>\\s*<log\\s*value=\"([^\"]*)\"\\s*/>\\s*</data>", |
| 172 |
G_REGEX_RAW, 0, NULL); |
| 173 |
g_assert (r); |
| 174 |
|
| 175 |
if (!g_regex_match (r, rsp, 0, &match_info)) |
| 176 |
goto out; |
| 177 |
|
| 178 |
if (!g_match_info_matches (match_info)) |
| 179 |
goto out; |
| 180 |
|
| 181 |
if (value) |
| 182 |
*value = g_match_info_fetch (match_info, 1); |
| 183 |
|
| 184 |
success = TRUE; |
| 185 |
|
| 186 |
out: |
| 187 |
if (match_info) |
| 188 |
g_match_info_unref (match_info); |
| 189 |
g_regex_unref (r); |
| 190 |
return success; |
| 191 |
} |
| 192 |
|
| 193 |
gboolean |
| 194 |
qfu_firehose_message_parse_response_configure (const gchar *rsp, |
| 195 |
guint32 *max_payload_size_to_target_in_bytes) |
| 196 |
{ |
| 197 |
GRegex *r; |
| 198 |
GMatchInfo *match_info = NULL; |
| 199 |
gboolean success = FALSE; |
| 200 |
|
| 201 |
/* |
| 202 |
* <?xml version="1.0" encoding="UTF-8" ?> |
| 203 |
* <data> |
| 204 |
* <response value="NAK" MemoryName="NAND" MaxPayloadSizeFromTargetInBytes="2048" MaxPayloadSizeToTargetInBytes="8192" MaxPayloadSizeToTargetInBytesSupported="8192" TargetName="9x55" /> |
| 205 |
* </data> |
| 206 |
*/ |
| 207 |
|
| 208 |
r = g_regex_new ("<data>\\s*<response\\s*(?:.*)MaxPayloadSizeToTargetInBytes=\"([^\"]*)\"(?:.*)/>\\s*</data>", |
| 209 |
G_REGEX_RAW, 0, NULL); |
| 210 |
g_assert (r); |
| 211 |
|
| 212 |
if (!g_regex_match (r, rsp, 0, &match_info)) |
| 213 |
goto out; |
| 214 |
|
| 215 |
if (!g_match_info_matches (match_info)) |
| 216 |
goto out; |
| 217 |
|
| 218 |
if (max_payload_size_to_target_in_bytes) { |
| 219 |
gchar *aux; |
| 220 |
|
| 221 |
aux = g_match_info_fetch (match_info, 1); |
| 222 |
*max_payload_size_to_target_in_bytes = atoi (aux); |
| 223 |
g_free (aux); |
| 224 |
} |
| 225 |
|
| 226 |
success = TRUE; |
| 227 |
|
| 228 |
out: |
| 229 |
if (match_info) |
| 230 |
g_match_info_unref (match_info); |
| 231 |
g_regex_unref (r); |
| 232 |
return success; |
| 233 |
} |