123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /*
- * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
- * This program is free software. It comes without any warranty, to the extent
- * permitted by applicable law. You can redistribute it and/or modify it under
- * the terms of the Do What The Fuck You Want To Public License, Version 2, as
- * published by Sam Hocevar. See the COPYING file for more details.
- */
- #include "flashsim.h"
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <assert.h>
- #ifdef FLASHSIM_LOG
- #define logprintf(args...) printf(args)
- #else
- #define logprintf(args...) do {} while (0)
- #endif
- struct flashsim {
- int size;
- int sector_size;
- FILE *fh;
- };
- struct flashsim *flashsim_open(const char *name, int size, int sector_size)
- {
- struct flashsim *sim = malloc(sizeof(struct flashsim));
- sim->size = size;
- sim->sector_size = sector_size;
- sim->fh = fopen(name, "w+");
- assert(sim->fh != NULL);
- assert(ftruncate(fileno(sim->fh), size) == 0);
- return sim;
- }
- void flashsim_close(struct flashsim *sim)
- {
- fclose(sim->fh);
- free(sim);
- }
- void flashsim_sector_erase(struct flashsim *sim, int addr)
- {
- int sector_start = addr - (addr % sim->sector_size);
- logprintf("flashsim_erase (0x%08x) * erasing sector at 0x%08x\n", addr, sector_start);
- void *empty = malloc(sim->sector_size);
- memset(empty, 0xff, sim->sector_size);
- assert(fseek(sim->fh, sector_start, SEEK_SET) == 0);
- assert(fwrite(empty, 1, sim->sector_size, sim->fh) == (size_t) sim->sector_size);
- free(empty);
- }
- void flashsim_read(struct flashsim *sim, int addr, uint8_t *buf, int len)
- {
- assert(fseek(sim->fh, addr, SEEK_SET) == 0);
- assert(fread(buf, 1, len, sim->fh) == (size_t) len);
- logprintf("flashsim_read (0x%08x) = %d bytes [ ", addr, len);
- for (int i=0; i<len; i++) {
- logprintf("%02x ", buf[i]);
- if (i == 15) {
- logprintf("... ");
- break;
- }
- }
- logprintf("]\n");
- }
- void flashsim_program(struct flashsim *sim, int addr, const uint8_t *buf, int len)
- {
- logprintf("flashsim_program(0x%08x) + %d bytes [ ", addr, len);
- for (int i=0; i<len; i++) {
- logprintf("%02x ", buf[i]);
- if (i == 15) {
- logprintf("... ");
- break;
- }
- }
- logprintf("]\n");
- uint8_t *data = malloc(len);
- assert(fseek(sim->fh, addr, SEEK_SET) == 0);
- assert(fread(data, 1, len, sim->fh) == (size_t) len);
- for (int i=0; i<(int) len; i++)
- data[i] &= buf[i];
- assert(fseek(sim->fh, addr, SEEK_SET) == 0);
- assert(fwrite(data, 1, len, sim->fh) == (size_t) len);
- free(data);
- }
- /* vim: set ts=4 sw=4 et: */
|