   }
   buf_pos += byte_count / 4;
 
-  // Endian swap.  Is there a better way than doing this?  The ROM data on disk on the host machine
-  // is big-endian.
-  for (uint32_t pos = 0; pos < (range_end - range_start); ++pos) {
-    read_buf[pos] =
-      ((read_buf[pos] & 0xFF000000L) >> 24)
-      | ((read_buf[pos] & 0xFF0000L) >> 8)
-      | ((read_buf[pos] & 0xFF00L) << 8)
-      | ((read_buf[pos] & 0xFFL) << 24);
-  }
+  // Endian swap.  Disabled this because I think the data coming from the host may
+  // already have the correct endianness.
+
+  // for (uint32_t pos = 0; pos < (range_end - range_start); ++pos) {
+  //   read_buf[pos] =
+  //     ((read_buf[pos] & 0xFF000000L) >> 24)
+  //     | ((read_buf[pos] & 0xFF0000L) >> 8)
+  //     | ((read_buf[pos] & 0xFF00L) << 8)
+  //     | ((read_buf[pos] & 0xFFL) << 24);
+  // }
 
   return true;
 }

  }
  // leave flash accessible by host machine
  flash_unlock();
}

uint32_t chip_size() {


      // Get one chunk of data from the remote host
      if (!get_range(chunk_start, chunk_start + CHUNK_SIZE)) return;

      // Program it into the flash, 512 bytes (128 words) at a time
      for (uint32_t program_offset = 0;
          program_offset < CHUNK_SIZE;
          program_offset += PROGRAM_BLOCK_SIZE) {

        uint32_t program_start = chunk_start + program_offset;

        Serial.print("Program at ");
        Serial.println(program_start);
        // Serial.print("first word in buf: ");
        // Serial.println(read_buf[0], HEX);
        // Enter Write Buffer Programming mode
        flash_write_both(0x555, 0xAA); // (1) Unlock 1
        flash_write_both(0x2AA, 0x55); // (2) Unlock 2
        flash_write_both(program_start, 0x25); // (3) Write Buffer Load
        flash_write_both(program_start, PROGRAM_BLOCK_SIZE - 1); // (4) Sector address + Number of word locations to program minus one
        for (uint32_t pos = 0; pos < PROGRAM_BLOCK_SIZE; ++pos) {
          flash_write(program_start + pos, read_buf[program_offset + pos]);
        }
        flash_write_both(program_start, 0x29); // (n+5) Program Buffer to Flash

        Serial.println("Wait for completion");
        // Poll last address written to buffer (program_start + PROGRAM_BLOCK_SIZE - 1), looking at DQ7/6/5/1
        uint32_t poll_comparison_value = read_buf[program_offset + PROGRAM_BLOCK_SIZE-1] & 0x00800080L;  // Compare with DQ7
        // Serial.print("Final word: ");
        // Serial.println(read_buf[PROGRAM_BLOCK_SIZE-1], HEX);
        uint32_t status_addr = program_start + PROGRAM_BLOCK_SIZE - 1;
        while (1) {
          uint32_t status = flash_read(status_addr);
          // Serial.print("Status & 80: ");
          // Serial.print(status & 0x00800080L, HEX);
          // Serial.print(" c.f. this: ");
          // Serial.println(poll_comparison_value, HEX);
          // Check for final state on both chips
          if ((status & 0x00800080L) == poll_comparison_value) {
            // We're done
            // Serial.println("got it");
            break;
          }
          if (check_disconnect()) return;

          // Check both chips independently for failure states
          for (uint32_t shift = 0; shift < 17; shift += 16) {
            uint32_t mask = 0xFFFFL << shift;

            uint32_t status = flash_read(status_addr);
            if ((status & 0x00800080L & mask) == (poll_comparison_value & mask)) {
              // This chip is done
              continue;
            }

            // Not done - check DQ5
            if (!(status & 0x00200020L & mask)) {
              // DQ5 == 0; check DQ1
              if (!(status & 0x00020002L & mask)) {
                // DQ1 == 0; continue
                continue;
              }
            }

            // Either DQ5 == 1 or DQ1 == 1; double check this isn't a glitch
            status = flash_read(status_addr);
            if ((status & 0x00800080L & mask) == (poll_comparison_value & mask)) {
              // This chip is done
              continue;
            }

            Serial.print("Failure state: read status==");
            Serial.println(status & mask, HEX);
            Serial.print(status & 0x00800080L & mask, HEX);

            Serial.print(" and poll comparison value is ");
            Serial.println(poll_comparison_value & mask, HEX);

            Serial.print("Actual value we're writing: ");
            Serial.println(read_buf[program_offset + PROGRAM_BLOCK_SIZE-1], HEX);

            // Either DQ5 or DQ1 indicated an error -- fail and reset
            if (status & 0x00200020L & mask) {
              // Device failed
              Serial.println("ERR Flash programming failed - exceeded program time limit");
              Serial.println(status, HEX);
              Serial.println(mask, HEX);
            } else if (status & 0x00020002L & mask) {
              // Operation aborted
              Serial.println("ERR Flash programming aborted");
              Serial.println(status, HEX);
              Serial.println(mask, HEX);
              // Write write-to-buffer-abort command
              flash_write_both(0x555, 0xAA);
              flash_write_both(0x2AA, 0x55);
              flash_write_both(0x555, 0xF0);
            }
            // Write reset command
            flash_write_both(0, 0xF0);
            // Just to be safe...
            flash_reset();
            return;
          }
        }
        // Block programmed
      }
      // If we got here, the chunk is programmed and we can continue
      Serial.println("Chunk done");
