| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2011-2013 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <glib.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include "ch-inhx32.h" |
| 29 |
|
| 30 |
#define CH_RECORD_TYPE_DATA 0 |
| 31 |
#define CH_RECORD_TYPE_EOF 1 |
| 32 |
#define CH_RECORD_TYPE_EXTENDED 4 |
| 33 |
|
| 34 |
static guint8 |
| 35 |
ch_inhx32_parse_uint8 (const gchar *data, guint pos) |
| 36 |
{ |
| 37 |
gchar buffer[3]; |
| 38 |
buffer[0] = data[pos+0]; |
| 39 |
buffer[1] = data[pos+1]; |
| 40 |
buffer[2] = '\0'; |
| 41 |
return g_ascii_strtoull (buffer, NULL, 16); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* ch_inhx32_to_bin_full: |
| 46 |
* @in_buffer: A %NULL terminated Intel hex byte string |
| 47 |
* @out_buffer: The output byte buffer |
| 48 |
* @out_size: The size of @out_buffer |
| 49 |
* @runcode_addr: The runcode address of the firmware |
| 50 |
* @error: A #GError or %NULL |
| 51 |
* |
| 52 |
* Converts the Intel hex byte string into a binary packed |
| 53 |
* representation suitable for direct flashing the ColorHug. |
| 54 |
* |
| 55 |
* Return value: packed value to host byte order |
| 56 |
* |
| 57 |
* Since: 1.2.9 |
| 58 |
**/ |
| 59 |
gboolean |
| 60 |
ch_inhx32_to_bin_full (const gchar *in_buffer, |
| 61 |
guint8 **out_buffer, |
| 62 |
gsize *out_size, |
| 63 |
guint16 runcode_addr, |
| 64 |
GError **error) |
| 65 |
{ |
| 66 |
gboolean verbose; |
| 67 |
gchar *ptr; |
| 68 |
gint checksum; |
| 69 |
gint end; |
| 70 |
gint i; |
| 71 |
gint offset = 0; |
| 72 |
guint8 data_tmp; |
| 73 |
guint addr32 = 0; |
| 74 |
guint addr32_last = 0; |
| 75 |
guint addr_high = 0; |
| 76 |
guint addr_low = 0; |
| 77 |
guint j; |
| 78 |
guint len_tmp; |
| 79 |
guint type; |
| 80 |
g_autoptr(GString) string = NULL; |
| 81 |
|
| 82 |
g_return_val_if_fail (in_buffer != NULL, FALSE); |
| 83 |
g_return_val_if_fail (runcode_addr > 0, FALSE); |
| 84 |
|
| 85 |
/* only if set */ |
| 86 |
verbose = g_getenv ("VERBOSE") != NULL; |
| 87 |
string = g_string_new (""); |
| 88 |
while (TRUE) { |
| 89 |
|
| 90 |
/* length, 16-bit address, type */ |
| 91 |
if (sscanf (&in_buffer[offset], ":%02x%04x%02x", |
| 92 |
&len_tmp, &addr_low, &type) != 3) { |
| 93 |
g_set_error_literal (error, 1, 0, |
| 94 |
"invalid inhx32 syntax"); |
| 95 |
return FALSE; |
| 96 |
} |
| 97 |
|
| 98 |
/* position of checksum */ |
| 99 |
end = offset + 9 + len_tmp * 2; |
| 100 |
|
| 101 |
/* verify checksum */ |
| 102 |
checksum = 0; |
| 103 |
for (i = offset + 1; i < end; i += 2) { |
| 104 |
data_tmp = ch_inhx32_parse_uint8 (in_buffer, i); |
| 105 |
checksum = (checksum + (0x100 - data_tmp)) & 0xff; |
| 106 |
} |
| 107 |
if (ch_inhx32_parse_uint8 (in_buffer, end) != checksum) { |
| 108 |
g_set_error_literal (error, 1, 0, |
| 109 |
"invalid checksum"); |
| 110 |
return FALSE; |
| 111 |
} |
| 112 |
|
| 113 |
/* process different record types */ |
| 114 |
switch (type) { |
| 115 |
case CH_RECORD_TYPE_DATA: |
| 116 |
/* if not contiguous with previous record, |
| 117 |
* issue accumulated hex data (if any) and start anew. */ |
| 118 |
if ((addr_high + addr_low) != addr32) |
| 119 |
addr32 = addr_high + addr_low; |
| 120 |
|
| 121 |
/* Parse bytes from line into hexBuf */ |
| 122 |
for (i = offset + 9; i < end; i += 2) { |
| 123 |
if (addr32 >= runcode_addr && |
| 124 |
addr32 < 0xfff0) { |
| 125 |
|
| 126 |
/* find out if there are any |
| 127 |
* holes in the hex record */ |
| 128 |
len_tmp = addr32 - addr32_last; |
| 129 |
if (addr32_last > 0x0 && len_tmp > 1) { |
| 130 |
for (j = 1; j < len_tmp; j++) { |
| 131 |
if (verbose) { |
| 132 |
g_debug ("Filling address 0x%04x", |
| 133 |
addr32_last + j); |
| 134 |
} |
| 135 |
/* although 0xff might be clearer, |
| 136 |
* we can't write 0xffff to pic14 */ |
| 137 |
g_string_append_c (string, 0x00); |
| 138 |
} |
| 139 |
} |
| 140 |
data_tmp = ch_inhx32_parse_uint8 (in_buffer, i); |
| 141 |
g_string_append_c (string, data_tmp); |
| 142 |
if (verbose) |
| 143 |
g_debug ("Writing address 0x%04x", addr32); |
| 144 |
addr32_last = addr32; |
| 145 |
} else { |
| 146 |
if (verbose) |
| 147 |
g_debug ("Ignoring address 0x%04x", addr32); |
| 148 |
} |
| 149 |
addr32++; |
| 150 |
} |
| 151 |
break; |
| 152 |
case CH_RECORD_TYPE_EOF: |
| 153 |
break; |
| 154 |
case CH_RECORD_TYPE_EXTENDED: |
| 155 |
if (sscanf (&in_buffer[offset+9], "%04x", &addr_high) != 1) { |
| 156 |
g_set_error_literal (error, 1, 0, |
| 157 |
"invalid hex syntax"); |
| 158 |
return FALSE; |
| 159 |
} |
| 160 |
addr_high <<= 16; |
| 161 |
addr32 = addr_high + addr_low; |
| 162 |
break; |
| 163 |
default: |
| 164 |
g_set_error_literal (error, 1, 0, |
| 165 |
"invalid record type"); |
| 166 |
return FALSE; |
| 167 |
} |
| 168 |
|
| 169 |
/* advance to start of next line */ |
| 170 |
ptr = strchr (&in_buffer[end+2], ':'); |
| 171 |
if (ptr == NULL) |
| 172 |
break; |
| 173 |
offset = ptr - in_buffer; |
| 174 |
} |
| 175 |
|
| 176 |
/* pad out to device size so we can read back a verifiable blob */ |
| 177 |
for (i = string->len; i < runcode_addr; i++) |
| 178 |
g_string_append_len (string, "\0", 1); |
| 179 |
|
| 180 |
/* save data */ |
| 181 |
if (out_size != NULL) |
| 182 |
*out_size = string->len; |
| 183 |
if (out_buffer != NULL) |
| 184 |
*out_buffer = g_memdup (string->str, string->len); |
| 185 |
return TRUE; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* ch_inhx32_to_bin: |
| 190 |
* @in_buffer: A %NULL terminated Intel hex byte string |
| 191 |
* @out_buffer: The output byte buffer |
| 192 |
* @out_size: The size of @out_buffer |
| 193 |
* @error: A #GError or %NULL |
| 194 |
* |
| 195 |
* Converts the Intel hex byte string into a binary packed |
| 196 |
* representation suitable for direct flashing the ColorHug. |
| 197 |
* |
| 198 |
* Return value: packed value to host byte order |
| 199 |
* |
| 200 |
* Since: 0.1.29 |
| 201 |
**/ |
| 202 |
gboolean |
| 203 |
ch_inhx32_to_bin (const gchar *in_buffer, |
| 204 |
guint8 **out_buffer, |
| 205 |
gsize *out_size, |
| 206 |
GError **error) |
| 207 |
{ |
| 208 |
return ch_inhx32_to_bin_full (in_buffer, out_buffer, out_size, |
| 209 |
CH_EEPROM_ADDR_RUNCODE, |
| 210 |
error); |
| 211 |
} |