flashsim.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. #include "flashsim.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14. #ifdef FLASHSIM_LOG
  15. #define logprintf(args...) printf(args)
  16. #else
  17. #define logprintf(args...) do {} while (0)
  18. #endif
  19. struct flashsim {
  20. int size;
  21. int sector_size;
  22. FILE *fh;
  23. };
  24. struct flashsim *flashsim_open(const char *name, int size, int sector_size)
  25. {
  26. struct flashsim *sim = malloc(sizeof(struct flashsim));
  27. sim->size = size;
  28. sim->sector_size = sector_size;
  29. sim->fh = fopen(name, "w+");
  30. assert(sim->fh != NULL);
  31. assert(ftruncate(fileno(sim->fh), size) == 0);
  32. return sim;
  33. }
  34. void flashsim_close(struct flashsim *sim)
  35. {
  36. fclose(sim->fh);
  37. free(sim);
  38. }
  39. void flashsim_sector_erase(struct flashsim *sim, int addr)
  40. {
  41. int sector_start = addr - (addr % sim->sector_size);
  42. logprintf("flashsim_erase (0x%08x) * erasing sector at 0x%08x\n", addr, sector_start);
  43. void *empty = malloc(sim->sector_size);
  44. memset(empty, 0xff, sim->sector_size);
  45. assert(fseek(sim->fh, sector_start, SEEK_SET) == 0);
  46. assert(fwrite(empty, 1, sim->sector_size, sim->fh) == (size_t) sim->sector_size);
  47. free(empty);
  48. }
  49. void flashsim_read(struct flashsim *sim, int addr, uint8_t *buf, int len)
  50. {
  51. assert(fseek(sim->fh, addr, SEEK_SET) == 0);
  52. assert(fread(buf, 1, len, sim->fh) == (size_t) len);
  53. logprintf("flashsim_read (0x%08x) = %d bytes [ ", addr, len);
  54. for (int i=0; i<len; i++) {
  55. logprintf("%02x ", buf[i]);
  56. if (i == 15) {
  57. logprintf("... ");
  58. break;
  59. }
  60. }
  61. logprintf("]\n");
  62. }
  63. void flashsim_program(struct flashsim *sim, int addr, const uint8_t *buf, int len)
  64. {
  65. logprintf("flashsim_program(0x%08x) + %d bytes [ ", addr, len);
  66. for (int i=0; i<len; i++) {
  67. logprintf("%02x ", buf[i]);
  68. if (i == 15) {
  69. logprintf("... ");
  70. break;
  71. }
  72. }
  73. logprintf("]\n");
  74. uint8_t *data = malloc(len);
  75. assert(fseek(sim->fh, addr, SEEK_SET) == 0);
  76. assert(fread(data, 1, len, sim->fh) == (size_t) len);
  77. for (int i=0; i<(int) len; i++)
  78. data[i] &= buf[i];
  79. assert(fseek(sim->fh, addr, SEEK_SET) == 0);
  80. assert(fwrite(data, 1, len, sim->fh) == (size_t) len);
  81. free(data);
  82. }
  83. /* vim: set ts=4 sw=4 et: */