Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * See https://pastebin.com/i3z2Ts50
- *
- * Copyright 2018 strict-flower
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- *
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include "hidapi.h"
- #include "libmcp2221.h"
- #include "avr_writer.h"
- mcp2221_t *g_dev;
- mcp2221_gpioconfset_t g_gpio_conf;
- int g_is_gpio_init = 0;
- // ATMega328P
- unsigned int g_flush_page_size = 64 * 2;
- unsigned int g_page_max = 256;
- unsigned int g_flush_size = 64 * 2 * 256;
- void writer_exit(int status_code) {
- ihex_list_free();
- mcp2221_exit();
- exit(status_code);
- }
- void init_device() {
- if (mcp2221_init() != MCP2221_SUCCESS) {
- fputs("!!! Init Error !!!\n", stderr);
- getchar();
- writer_exit(1);
- }
- int count = mcp2221_find(MCP2221_DEFAULT_VID, MCP2221_DEFAULT_PID, NULL, NULL, NULL);
- if (count != 1) {
- fputs("[-] You need connect one MCP2221A\n", stderr);
- writer_exit(1);
- }
- g_dev = mcp2221_open();
- if (!g_dev) {
- fputs("[-] Failed to open the device\n", stderr);
- getchar();
- writer_exit(1);
- }
- }
- int main(int ac, char **av) {
- if (ac != 2) {
- fprintf(stderr, "Usage: %s hogefuga.hex\n", av[0]);
- exit(0);
- }
- puts("[+] Initializing device...");
- init_device();
- puts("[+] Initializing GPIO...");
- gpio_init();
- puts("[+] Transitioning AVR to Programming mode");
- avr_start_prog();
- avr_wait_prog();
- puts("[+] Loading ihex...");
- ihex_load(av[1]);
- printf("[+] Fuse Lower: %02x\n", avr_transaction(0x50, 0x00, 0x00, 0x00));
- printf("[+] Fuse Upper: %02x\n", avr_transaction(0x58, 0x08, 0x00, 0x00));
- puts("[+] Erasing...");
- avr_erase();
- puts("[+] Writing...");
- ihex_record_list_t *cur = g_ihex_record_list_root;
- while (cur != NULL) {
- if (cur->record.record_type == IHEX_EOF) {
- break;
- } else if (cur->record.record_type == IHEX_DATA) {
- for (unsigned int i = 0; i < cur->record.byte_count; i += 2) {
- byte upper_byte = cur->record.data[i];
- byte lower_byte = cur->record.data[i + 1];
- avr_flash_set_word((cur->record.address + i) / 2, upper_byte, lower_byte);
- }
- } else {
- // TODO: not implemented yet
- ASSERT(0);
- }
- cur = cur->next;
- }
- avr_flush();
- puts("[+] Verifying...");
- cur = g_ihex_record_list_root;
- while (cur != NULL) {
- if (cur->record.record_type == IHEX_EOF) {
- break;
- } else if (cur->record.record_type == IHEX_DATA) {
- for (unsigned int i = 0; i < cur->record.byte_count; i += 2) {
- byte upper_byte = cur->record.data[i];
- byte lower_byte = cur->record.data[i + 1];
- word dat = upper_byte * 256 + lower_byte;
- word flash = avr_flash_read((cur->record.address + i) / 2);
- if (flash != upper_byte * 256 + lower_byte) {
- fputs("[-] Verify Failed...\n", stderr);
- printf("Address: %04x, Expected: %04x, Result: %04x\n", (cur->record.address + i) / 2, dat, flash);
- writer_exit(1);
- }
- }
- } else {
- // TODO: not implemented yet
- ASSERT(0);
- }
- cur = cur->next;
- }
- puts("[+] Done.");
- writer_exit(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment