int emmc_write(char *side, char **value) { int fd, i; char buf[MACADDR_LEN]; long long size, start_addr; fd = open(EMMC_PARTITION, O_RDWR | O_SYNC); if(fd < 0) { fprintf(stderr, "Could not open emmc device: %s\n", EMMC_PARTITION); exit(1); return -1; } if (get_start_address(&size, &start_addr) < 0) { close(fd); exit(1); } if (WAN_OFFSET+MACADDR_LEN >= size) { fprintf(stderr, "exceed than the maximum size: %llx\n", size); close(fd); exit(1); } lseek(fd, start_addr+keyfromstring(side), SEEK_SET); for (i = 0; i < MACADDR_LEN; i++) buf[i] = strtol(value[i], NULL, 16); if (write(fd, buf, MACADDR_LEN) != MACADDR_LEN) { fprintf(stderr, "write() %s failed\n", EMMC_PARTITION); close(fd); return -1; } close(fd); return 0; }
int emmc_read(char *side) { int fd, pos_emmc, pos_file, i; char mac_addr[MACADDR_LEN]; long long size, start_addr; fd = open(EMMC_PARTITION, O_RDWR | O_SYNC); if(fd < 0) { fprintf(stderr, "Could not open emmc device: %s\n", EMMC_PARTITION); exit(1); return -1; } if (get_start_address(&size, &start_addr) < 0) { close(fd); exit(1); } lseek(fd, start_addr+keyfromstring(side), SEEK_SET); if(read(fd, mac_addr, MACADDR_LEN) != MACADDR_LEN){ printf("read() failed\n"); close(fd); return -1; } for (i = 0; i < MACADDR_LEN; i++) { printf("%02X", mac_addr[i]); if (i < MACADDR_LEN-1) printf(":"); else printf("\n"); } close(fd); return 0; }
cs1550_directory_entry move_file(cs1550_directory_entry directory, int file_index) //moves the given file { FILE *disk; cs1550_disk_block block; //block that holds information from parts of disk bitmap bitmap; //bitmap to read, edit and write long new_address = get_start_address( directory, file_index ); long count = 0; disk = fopen(".disk", "r+b"); fseek(disk, -1 * sizeof(bitmap), SEEK_END); //seeks to end and -1 size of bitmap to read in fread(&bitmap, sizeof(bitmap), 1, disk); //read in bitmap while( count <= directory.files[ file_index ].fsize ) //note that this fsize is the old offset { fseek(disk, directory.files[ file_index ].nStartBlock + count, SEEK_SET); //seek from start to where current block is fread(&block.data, sizeof(char), sizeof(block.data), disk); //read in a block bitmap.tracker[ (directory.files[ file_index ].nStartBlock + count) / 512 ] = '0'; //sets the block to free fseek(disk, new_address + count, SEEK_SET); //seek from start to new block on disk fwrite(&block.data, sizeof(char), sizeof(block.data), disk); //write data to new block bitmap.tracker[ (new_address + count) / 512 ] = '1'; //sets the new block to used count += 512; //increment count } fseek(disk, -1 * sizeof(bitmap), SEEK_END); //seeks to end and -1 size of bitmap to read in fwrite(&bitmap, sizeof(bitmap), 1, disk); //writes into bitmap fclose(disk); directory.files[ file_index ].nStartBlock = new_address; //puts new address in entry return directory; }
int emmc_write_offset(char *side, const unsigned int offset, char *value) { int fd, i, offset_t, len; //char buf[MACADDR_LEN]; unsigned char *buf; long long size, start_addr; fd = open(EMMC_PARTITION, O_RDWR | O_SYNC); if(fd < 0) { fprintf(stderr, "Could not open emmc device: %s\n", EMMC_PARTITION); exit(1); return -1; } if (get_start_address(&size, &start_addr) < 0) { close(fd); exit(1); } if (WAN_OFFSET+MACADDR_LEN >= size) { fprintf(stderr, "exceed than the maximum size: %llx\n", size); close(fd); exit(1); } offset_t = get_offset(side); if(offset_t < 0){ fprintf(stderr, "wrong partition name: %s\n", side); close(fd); exit(1); } offset_t += offset; len = strlen(value); len = (len / 2) + (len % 2); if ((offset_t + len) >= size) { fprintf(stderr, "exceed than the maximum size: %llx\n", size); close(fd); exit(1); } /* if (!strcmp(side, "wan")) lseek(fd, start_addr+WAN_OFFSET, SEEK_SET); else lseek(fd, start_addr+LAN_OFFSET, SEEK_SET); */ lseek(fd, start_addr + offset_t, SEEK_SET); buf = malloc(len * sizeof(char)); for (i = 0; i < len; i++){ buf[i] = get_str_byte_value(&value[i*2]); } if (write(fd, buf, MACADDR_LEN) != MACADDR_LEN) { fprintf(stderr, "write() %s failed\n", EMMC_PARTITION); close(fd); return -1; } printf("write %s[%08X]=%s(%02X%02X)\n", side, offset_t, value, buf[0], buf[1]); close(fd); return 0; }
int emmc_read_offset(char *side, const unsigned int offset, const unsigned int len) { int fd, pos_emmc, pos_file, i, offset_t; char mac_addr[MACADDR_LEN]; long long size, start_addr; char *ptr; fd = open(EMMC_PARTITION, O_RDWR | O_SYNC); if(fd < 0) { fprintf(stderr, "Could not open emmc device: %s\n", EMMC_PARTITION); exit(1); return -1; } if (get_start_address(&size, &start_addr) < 0) { close(fd); exit(1); } offset_t = get_offset(side); if(offset_t < 0){ fprintf(stderr, "wrong partition name: %s\n", side); close(fd); exit(1); } offset_t += offset; if ((offset_t + len) >= size) { fprintf(stderr, "exceed than the maximum size: %llx\n", size); close(fd); exit(1); } /* if (!strcmp(side, "wan")) lseek(fd, start_addr+WAN_OFFSET, SEEK_SET); else (!strcmp(side, "lan")) lseek(fd, start_addr+LAN_OFFSET, SEEK_SET); */ lseek(fd, start_addr + offset_t, SEEK_SET); if(read(fd, mac_addr, len) != len){ printf("read() failed\n"); close(fd); return -1; } printf("read %s[%08X]=", side, offset_t); for (i = 0; i < len; i++) { printf("%02X", mac_addr[i]); if (i < len-1) printf(":"); else printf("\n"); } close(fd); return 0; }
int main(int argc, char* argv[]) { if (argc != 3) { printf("usage:\n"); printf(" elf2tinyapp elf_file app_file\n"); return -1; } const char *elf_file = argv[1]; const char *app_file = argv[2]; printf("elf file: %s\n", elf_file); printf("app file: %s\n", app_file); struct ELF_SECTION text, rodata, data, bss; memset(&text, 0, sizeof(struct ELF_SECTION)); memset(&rodata, 0, sizeof(struct ELF_SECTION)); memset(&data, 0, sizeof(struct ELF_SECTION)); memset(&bss, 0, sizeof(struct ELF_SECTION)); text.name = ".text"; rodata.name = ".rodata"; data.name = ".data"; bss.name = ".bss"; read_section(elf_file, &text); read_section(elf_file, &rodata); read_section(elf_file, &data); read_section(elf_file, &bss); printf("text size: %d\n", text.size); printf("rodata size: %d\n", rodata.size); printf("data size: %d\n", data.size); printf("bss size: %d\n", bss.size); assert(text.size > 0); assert(rodata.size >= 0); assert(data.size >= 0); assert(bss.size >= 0); // app header struct APP_HEADER header; assert(sizeof(header) == 36); int inital_data_size = 0; if (rodata.size > 0) { inital_data_size = rodata.VMA + rodata.size - 0x310000; } if (data.size > 0) { inital_data_size = data.VMA + data.size - 0x310000; } if (bss.size > 0) { inital_data_size = bss.VMA + bss.size - 0x310000; } assert(inital_data_size >= rodata.size + data.size + bss.size); header.total_size = 0x311000; memcpy(header.sign, "tiny", 4); header.mmarea_size = 0; header.data_addr = 0x310000; header.data_size = inital_data_size; header.inital_data_pos = 0; header.opcode = 0xe9000000; header.entry_addr = (unsigned int)get_start_address(elf_file) - 0x20; header.heap_addr = (header.data_addr + inital_data_size + 3) / 4 * 4; // generate tinyapp file FILE *app_fp = fopen(app_file, "wb"); assert(app_fp != 0); write_header(app_fp, &header); write_section(app_fp, &header, &text); write_align4(app_fp); header.inital_data_pos = ftell(app_fp); write_section(app_fp, &header, &rodata); write_section(app_fp, &header, &data); write_section(app_fp, &header, &bss); // update tinyapp header write_header(app_fp, &header); fclose(app_fp); return 0; }