1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
//
// srecord - Manipulate EPROM load files
// Copyright (C) 2011 Peter Miller
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <srecord/arglex/tool.h>
#include <srecord/input/file/idt.h>
#include <srecord/record.h>
srecord::input_file_idt::~input_file_idt()
{
}
srecord::input_file_idt::input_file_idt(
const std::string &a_file_name
) :
input_file(a_file_name),
data_count(0),
seen_some_input(false)
{
}
srecord::input_file::pointer
srecord::input_file_idt::create(const std::string &a_file_name)
{
return pointer(new input_file_idt(a_file_name));
}
void
srecord::input_file_idt::record_format_error(void)
{
fatal_error("record format error");
}
bool
srecord::input_file_idt::read_inner(record &result)
{
int c = get_char();
if (c < 0)
return false;
if (c != 'S')
record_format_error();
int tag = get_nibble();
if (tag < 0)
record_format_error();
unsigned char csum = 0;
int line_length = get_char();
if (line_length < 0)
record_format_error();
csum += line_length;
if (line_length < 1)
fatal_error("line length invalid");
record::data_t buffer[256];
for (int j = 0; j < line_length; ++j)
{
int c = get_char();
if (c < 0)
record_format_error();
buffer[j] = c;
csum += c;
}
if (use_checksums() && csum != 0xFF)
fatal_error("checksum mismatch (%02X != FF)", csum);
--line_length;
int naddr = 2;
record::type_t type = record::type_unknown;
switch (tag)
{
default:
record_format_error();
case 0:
// header
type = record::type_header;
if (line_length < naddr)
{
// Some programs write Very short headers.
naddr = line_length;
}
break;
case 1:
// data
type = record::type_data;
break;
case 2:
// data
type = record::type_data;
naddr = 3;
break;
case 3:
// data
type = record::type_data;
naddr = 4;
break;
case 5:
// data count
type = record::type_data_count;
//
// This is documented as having 2 address bytes and
// no data bytes. The Green Hills toolchain happily
// generates records with 4 address bytes. We cope
// with this silently.
//
if (line_length >= 2 && line_length <= 4)
naddr = line_length;
break;
case 6:
// data count
type = record::type_data_count;
//
// Just in case some smarty-pants uses the Green Hills
// trick, we cope with address size crap the same as S5.
//
naddr = 3;
if (line_length == 4)
naddr = line_length;
break;
case 7:
// termination
type = record::type_execution_start_address;
naddr = 4;
break;
case 8:
// termination
type = record::type_execution_start_address;
naddr = 3;
break;
case 9:
// termination
type = record::type_execution_start_address;
break;
}
if (line_length < naddr)
{
fatal_error
(
"data length too short (%d < %d) for data type (%x)",
line_length,
naddr,
tag
);
}
record::address_t addr = record::decode_big_endian(buffer, naddr);
result = record(type, addr, buffer + naddr, line_length - naddr);
return true;
}
bool
srecord::input_file_idt::read(record &record)
{
for (;;)
{
if (!read_inner(record))
{
if (!seen_some_input)
fatal_error("file contains no data");
return false;
}
seen_some_input = true;
switch (record.get_type())
{
case record::type_unknown:
fatal_error("record type not recognised");
break;
case record::type_header:
if (record.get_address())
{
warning("address in header record ignored");
record.set_address(0);
}
break;
case record::type_data:
++data_count;
if (record.get_length() == 0)
{
warning("empty data record ignored");
continue;
}
break;
case record::type_data_count:
{
record::address_t addr = record.get_address();
record::address_t mask = 0xFFFF;
while (addr > mask)
mask = ~(~mask << 8);
mask &= data_count;
if (addr != mask)
{
fatal_error
(
"data record count mismatch (file %ld, read %ld)",
(long)addr,
(long)mask
);
}
}
continue;
case record::type_execution_start_address:
if (record.get_length() > 0)
{
warning("data in termination record ignored");
record.set_length(0);
}
break;
}
break;
}
return true;
}
bool
srecord::input_file_idt::is_binary(void)
const
{
return true;
}
const char *
srecord::input_file_idt::get_file_format_name()
const
{
return "IDT System Integration Manager binary";
}
const char *
srecord::input_file_idt::format_option_name(void)
const
{
return "-Integrated_Device_Technology";
}
// vim: set ts=8 sw=4 et :
|