-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFirmware.cc
More file actions
107 lines (94 loc) · 2.26 KB
/
Firmware.cc
File metadata and controls
107 lines (94 loc) · 2.26 KB
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
// Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
//
// This file is part of SCSI2SD.
//
// SCSI2SD is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SCSI2SD 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 General Public License
// along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
#include "Firmware.hh"
extern "C"
{
#include "cybtldr_parse.h"
}
#include <functional>
#include <sstream>
#include <stdexcept>
#include <string.h>
using namespace SCSI2SD;
namespace
{
struct FirmwareFile
{
~FirmwareFile()
{
CyBtldr_CloseDataFile();
}
};
}
Firmware::Firmware(const std::string& path)
{
if (CyBtldr_OpenDataFile(path.c_str()) != CYRET_SUCCESS)
{
std::stringstream msg;
msg << "Could not open file: " << path;
throw std::runtime_error(msg.str());
}
FirmwareFile closeOnReturn;
uint8_t buffer[MAX_BUFFER_SIZE];
unsigned int lineSize;
if (
CyBtldr_ReadLine(&lineSize, reinterpret_cast<char*>(buffer))
!= CYRET_SUCCESS
)
{
std::stringstream msg;
msg << "Could not read file: " << path;
throw std::runtime_error(msg.str());
}
{
unsigned long silId;
unsigned char silRev[MAX_BUFFER_SIZE];
unsigned char chksum[MAX_BUFFER_SIZE];
if (
CyBtldr_ParseHeader(
lineSize,
buffer,
&silId,
silRev,
chksum)
!= CYRET_SUCCESS)
{
std::stringstream msg;
msg << "Premature end of file: " << path;
throw std::runtime_error(msg.str());
}
mySiliconId = silId;
mySiliconRev =
std::string(
reinterpret_cast<char*>(silRev),
strnlen(reinterpret_cast<char*>(silRev), MAX_BUFFER_SIZE)
);
}
// Count the number of flash rows. This is used to show "percentage
// complete" when uploading the firmware.
myTotalFlashRows = 0;
while (
CyBtldr_ReadLine(&lineSize, reinterpret_cast<char*>(buffer))
== CYRET_SUCCESS
)
{
myTotalFlashRows++;
}
}
Firmware::~Firmware()
{
}