51 #include <boost/format.hpp> 55 #define FLASH_SIZE (FLASH_PAGE_SIZE * (FLASH_MAX_PAGENO + 1) / 2) 78 file.open(fname.c_str());
79 std::auto_ptr<std::vector<uint8_t> >
data;
85 fprintf(stderr,
"Failed to open file %s\n", fname.c_str());
93 file.getline(line,
sizeof(line));
94 unsigned char *curchar = (
unsigned char *)line;
96 if (*curchar++ !=
':')
98 fprintf(stderr,
"Line %i did not start with a colon.\n", linenum);
107 while (isxdigit(curchar[0]) && isxdigit(curchar[1]))
109 bytes[bytecount] = 16 *
hexval(*curchar++);
110 bytes[bytecount] +=
hexval(*curchar++);
111 checksum += bytes[bytecount++];
114 while (*curchar && isspace(*curchar))
119 fprintf(stderr,
"Unexpected character 0x%02X at end of line %i.\n",
120 (
int) *curchar, linenum);
130 fprintf(stderr,
"Line %i was too short (%i < 5).\n", linenum, bytecount);
136 if (len != bytecount - 5)
138 fprintf(stderr,
"Line %i contained %i bytes, but reported %i bytes.\n",
139 linenum, bytecount, bytes[0]);
143 int addr = (bytes[1] << 8) + bytes[2];
150 fprintf(stderr,
"Non contiguous address (%08x != %08x), line %i\n",
156 fprintf(stderr,
"Exceeded reserved flash size (note: upload_mcs code can be edited to allow larger values) at line %i.\n", linenum);
159 for (
int i = 0; i < len; i++)
172 fprintf(stderr,
"EOF record on line %i was followed by character %02X.\n",
181 fprintf(stderr,
"Extended record had wrong length (%i != 2) on line %i\n",
185 curseg = ((bytes[4] << 8) + bytes[5]) << 16;
189 fprintf(stderr,
"Unknown record type %i at line %i.\n", bytes[3], linenum);
194 fprintf(stderr,
"Unexpected EOF after line %i.\n", linenum);
202 uint8_t swapped[256];
204 for (
int i = 0; i < 256; i++)
208 for (
int j = 0; j < 8; j++)
210 swapped[i] = (swapped[i] << 1) | (shift & 1);
225 fprintf(stderr,
"Searching for camera: %s", camera_url);
226 for (
int i = 0; i < 5; i++)
228 fprintf(stderr,
".");
232 fprintf(stderr,
"\n");
238 fprintf(stderr,
"\nMatching URL %s : %s\n", camera_url, errmsg);
246 fprintf(stderr,
"Unable to create ARP entry (are you root?), continuing anyway\n");
248 fprintf(stderr,
"IP address configuration failed\n");
254 fprintf(stderr,
"Could not communicate with camera after configuring IP. Is ARP set? Is %s accessible from %s?\n", camera.
ip_str, camera.
ifName);
258 fprintf(stderr,
"******************************************************************\n");
259 fprintf(stderr,
"Firmware file parsed successfully. Will start writing to flash in 5 seconds.\n");
260 fprintf(stderr,
"Make sure that no other software is accessing the camera during reflashing.\n");
261 fprintf(stderr,
"Hit CTRL+C to abort now.\n");
262 fprintf(stderr,
"******************************************************************\n");
265 fprintf(stderr,
"Writing to flash. DO NOT ABORT OR TURN OFF CAMERA!!\n");
274 fprintf(stderr,
".");
279 int startretries = 10;
280 int retries = startretries;
284 fprintf(stderr,
"\nFlash write error on page %i.\n", page);
285 fprintf(stderr,
"If you reset the camera it will probably not come up.\n");
286 fprintf(stderr,
"Try reflashing NOW, to possibly avoid a hard JTAG reflash.\n");
287 fprintf(stderr,
"Be sure that you are not streaming images. Reflash will not\n");
288 fprintf(stderr,
"work while streaming.\n");
292 if (retries < startretries)
294 fprintf(stderr,
"x(%i)\n", startretries - retries);
298 fprintf(stderr,
"\n");
300 fprintf(stderr,
"Verifying flash.\n");
308 fprintf(stderr,
".");
314 fprintf(stderr,
"\nFlash read error on page %i.\n", page);
315 fprintf(stderr,
"If you reset the camera it will probably not come up.\n");
316 fprintf(stderr,
"Try reflashing NOW, to possibly avoid a hard JTAG reflash.\n");
320 if (memcmp(buff, (uint8_t *)
firmware + addr, FLASH_PAGE_SIZE))
322 fprintf(stderr,
"\nFlash compare error on page %i.\n", page);
323 fprintf(stderr,
"If you reset the camera it will probably not come up.\n");
324 fprintf(stderr,
"Try reflashing NOW, to possibly avoid a hard JTAG reflash.\n");
328 fprintf(stderr,
"\n");
330 fprintf(stderr,
"Success! Restarting camera, should take about 10 seconds to come back up after this.\n");
337 int main(
int argc,
char **argv)
339 if ((argc != 3 && argc != 2) || !strcmp(argv[1],
"--help")) {
340 fprintf(stderr,
"Usage: %s <file.mcs> <camera_url>\n", argv[0]);
341 fprintf(stderr,
" %s <file.mcs> > dump.bin\n", argv[0]);
342 fprintf(stderr,
"Reads a .mcs file and uploads it to the camera or dumps it as binary to stdout.\n");
353 fprintf(stderr,
"Unexpected value at position 4. Don't know whether to bit-swap.\n");
363 char* camera_url = argv[2];
366 const int max_retries = 5;
367 for (
int i = 0; i < max_retries; i++)
372 fprintf(stderr,
"\nUpload failed. Retrying... %u/%u\n\n", i+1, max_retries);
int wge100ReliableFlashRead(const IpCamList *camInfo, uint32_t address, uint8_t *pageDataOut, int *retries)
int wge100ReliableFlashWrite(const IpCamList *camInfo, uint32_t address, const uint8_t *pageDataIn, int *retries)
int main(int argc, char **argv)
#define TRIG_STATE_INTERNAL
int wge100Configure(IpCamList *camInfo, const char *ipAddress, unsigned wait_us)
#define ERR_CONFIG_ARPFAIL
uint8_t firmware[FLASH_SIZE]
uint16_t checksum(uint16_t *data)
int wge100FindByUrl(const char *url, IpCamList *camera, unsigned wait_us, const char **errmsg)
int wge100TriggerControl(const IpCamList *camInfo, uint32_t triggerType)
int read_mcs(std::string fname)
int wge100ReconfigureFPGA(IpCamList *camInfo)
int write_flash(char *camera_url)