-void WaitForAck(void) {
- UsbCommand ack;
- ReceiveCommand(&ack);
- if(ack.cmd != CMD_ACK) {
- printf("bad ACK\n");
- exit(-1);
- }
-}
-
-struct partition {
- int start;
- int end;
- int precious;
- const char *name;
-};
-struct partition partitions[] = {
- {0x100000, 0x102000, 1, "bootrom"},
- {0x102000, 0x110000, 0, "fpga"},
- {0x110000, 0x140000, 0, "os"},
-};
-
-/* If translate is set, subtract PHYSICAL_FLASH_START to translate for old
- * bootroms.
- */
-static void FlushPrevious(int translate)
-{
- UsbCommand c;
- memset(&c, 0, sizeof(c));
-
- printf("expected = %08x flush, ", ExpectedAddr);
-
- int i;
- for(i = 0; i < 240; i += 48) {
- c.cmd = CMD_SETUP_WRITE;
- memcpy(c.d.asBytes, QueuedToSend+i, 48);
- c.arg[0] = (i/4);
- SendCommand(&c);
- WaitForAck();
- }
-
- c.cmd = CMD_FINISH_WRITE;
- c.arg[0] = (ExpectedAddr-1) & (~255);
- if(translate) {
- c.arg[0] -= PHYSICAL_FLASH_START;
- }
- printf("c.arg[0] = %08x\r", c.arg[0]);
- memcpy(c.d.asBytes, QueuedToSend+240, 16);
- SendCommand(&c);
- WaitForAck();
-
- AllWritten = true;
-}
-
-/* Where must be between start_addr (inclusive) and end_addr (exclusive).
- */
-static void GotByte(uint32_t where, uint8_t which, int start_addr, int end_addr, int translate)
-{
- AllWritten = false;
-
- if(where < start_addr || where >= end_addr) {
- printf("bad: got byte at %08x, outside of range %08x-%08x\n", where, start_addr, end_addr);
- exit(-1);
- }
-
- if(where != ExpectedAddr) {
- printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);
- exit(-1);
- }
- QueuedToSend[where & 255] = which;
- ExpectedAddr++;
-
- if((where & 255) == 255) {
- // we have completed a full page
- FlushPrevious(translate);
- }
-}
-
-static int HexVal(int c)
-{
- c = tolower(c);
- if(c >= '0' && c <= '9') {
- return c - '0';
- } else if(c >= 'a' && c <= 'f') {
- return (c - 'a') + 10;
- } else {
- printf("bad hex digit '%c'\n", c);
- exit(-1);
- }
-}
-
-static uint8_t HexByte(char *s)
-{
- return (HexVal(s[0]) << 4) | HexVal(s[1]);
-}