strict-flower

avr_writer.c

Nov 23rd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.18 KB | None | 0 0
  1. /*
  2.  * See https://pastebin.com/i3z2Ts50
  3.  *
  4.  * Copyright 2018 strict-flower
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22.  * IN THE SOFTWARE.
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "hidapi.h"
  31. #include "libmcp2221.h"
  32. #include "avr_writer.h"
  33.  
  34. mcp2221_t *g_dev;
  35. mcp2221_gpioconfset_t g_gpio_conf;
  36. int g_is_gpio_init = 0;
  37. // ATMega328P
  38. unsigned int g_flush_page_size = 64 * 2;
  39. unsigned int g_page_max = 256;
  40. unsigned int g_flush_size = 64 * 2 * 256;
  41.  
  42. void writer_exit(int status_code) {
  43.   ihex_list_free();
  44.   mcp2221_exit();
  45.   exit(status_code);
  46. }
  47.  
  48. void init_device() {
  49.   if (mcp2221_init() != MCP2221_SUCCESS) {
  50.     fputs("!!! Init Error !!!\n", stderr);
  51.     getchar();
  52.     writer_exit(1);
  53.   }
  54.  
  55.   int count = mcp2221_find(MCP2221_DEFAULT_VID, MCP2221_DEFAULT_PID, NULL, NULL, NULL);
  56.   if (count != 1) {
  57.     fputs("[-] You need connect one MCP2221A\n", stderr);
  58.     writer_exit(1);
  59.   }
  60.  
  61.   g_dev = mcp2221_open();
  62.   if (!g_dev) {
  63.     fputs("[-] Failed to open the device\n", stderr);
  64.     getchar();
  65.     writer_exit(1);
  66.   }
  67. }
  68.  
  69. int main(int ac, char **av) {
  70.   if (ac != 2) {
  71.     fprintf(stderr, "Usage: %s hogefuga.hex\n", av[0]);
  72.     exit(0);
  73.   }
  74.   puts("[+] Initializing device...");
  75.   init_device();
  76.   puts("[+] Initializing GPIO...");
  77.   gpio_init();
  78.  
  79.   puts("[+] Transitioning AVR to Programming mode");
  80.   avr_start_prog();
  81.   avr_wait_prog();
  82.  
  83.   puts("[+] Loading ihex...");
  84.   ihex_load(av[1]);
  85.  
  86.   printf("[+] Fuse Lower: %02x\n", avr_transaction(0x50, 0x00, 0x00, 0x00));
  87.   printf("[+] Fuse Upper: %02x\n", avr_transaction(0x58, 0x08, 0x00, 0x00));
  88.  
  89.   puts("[+] Erasing...");
  90.   avr_erase();
  91.  
  92.   puts("[+] Writing...");
  93.   ihex_record_list_t *cur = g_ihex_record_list_root;
  94.   while (cur != NULL) {
  95.     if (cur->record.record_type == IHEX_EOF) {
  96.       break;
  97.     } else if (cur->record.record_type == IHEX_DATA) {
  98.       for (unsigned int i = 0; i < cur->record.byte_count; i += 2) {
  99.         byte upper_byte = cur->record.data[i];
  100.         byte lower_byte = cur->record.data[i + 1];
  101.         avr_flash_set_word((cur->record.address + i) / 2, upper_byte, lower_byte);
  102.       }
  103.     } else {
  104.       // TODO: not implemented yet
  105.       ASSERT(0);
  106.     }
  107.     cur = cur->next;
  108.   }
  109.   avr_flush();
  110.  
  111.   puts("[+] Verifying...");
  112.   cur = g_ihex_record_list_root;
  113.   while (cur != NULL) {
  114.     if (cur->record.record_type == IHEX_EOF) {
  115.       break;
  116.     } else if (cur->record.record_type == IHEX_DATA) {
  117.       for (unsigned int i = 0; i < cur->record.byte_count; i += 2) {
  118.         byte upper_byte = cur->record.data[i];
  119.         byte lower_byte = cur->record.data[i + 1];
  120.         word dat = upper_byte * 256 + lower_byte;
  121.         word flash = avr_flash_read((cur->record.address + i) / 2);
  122.         if (flash != upper_byte * 256 + lower_byte) {
  123.           fputs("[-] Verify Failed...\n", stderr);
  124.           printf("Address: %04x, Expected: %04x, Result: %04x\n", (cur->record.address + i) / 2, dat, flash);
  125.           writer_exit(1);
  126.         }
  127.       }
  128.     } else {
  129.       // TODO: not implemented yet
  130.       ASSERT(0);
  131.     }
  132.     cur = cur->next;
  133.   }
  134.  
  135.   puts("[+] Done.");
  136.  
  137.   writer_exit(0);
  138.   return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment