Blame view

RIOT/cpu/native/mtd/mtd_native.c 3.51 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  /*
   * Copyright (C) 2016  OTA keys S.A.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   * @brief       mtd flash emulation for native
   *
   * @file
   *
   * @author      Vincent Dupont <vincent@otakeys.com>
   */
  
  #include <stdio.h>
  #include <assert.h>
  #include <inttypes.h>
  #include <errno.h>
  
  #include "mtd.h"
  #include "mtd_native.h"
  
  #include "native_internal.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  static int _init(mtd_dev_t *dev)
  {
      mtd_native_dev_t *_dev = (mtd_native_dev_t*) dev;
  
      DEBUG("mtd_native: init, filename=%s\n", _dev->fname);
  
      FILE *f = real_fopen(_dev->fname, "r");
  
      if (!f) {
          DEBUG("mtd_native: init: creating file %s\n", name);
          f = real_fopen(_dev->fname, "w+");
          if (!f) {
              return -EIO;
          }
          size_t size = dev->sector_count * dev->pages_per_sector * dev->page_size;
          for (size_t i = 0; i < size; i++) {
              real_fputc(0xff, f);
          }
      }
  
      real_fclose(f);
  
      return 0;
  }
  
  static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
  {
      mtd_native_dev_t *_dev = (mtd_native_dev_t*) dev;
      size_t mtd_size = dev->sector_count * dev->pages_per_sector * dev->page_size;
  
      DEBUG("mtd_native: read from page %" PRIu32 " count %" PRIu32 "\n", addr, size);
  
      if (addr + size > mtd_size) {
          return -EOVERFLOW;
      }
  
      FILE *f = real_fopen(_dev->fname, "r");
      if (!f) {
          return -EIO;
      }
      real_fseek(f, addr, SEEK_SET);
      size = real_fread(buff, 1, size, f);
      real_fclose(f);
  
      return size;
  }
  
  static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
  {
      mtd_native_dev_t *_dev = (mtd_native_dev_t*) dev;
      size_t mtd_size = dev->sector_count * dev->pages_per_sector * dev->page_size;
      size_t sector_size = dev->pages_per_sector * dev->page_size;
  
      DEBUG("mtd_native: write from page %" PRIu32 " count %" PRIu32 "\n", addr, size);
  
      if (addr + size > mtd_size) {
          return -EOVERFLOW;
      }
      if (((addr % sector_size) + size) > sector_size) {
          return -EOVERFLOW;
      }
  
      FILE *f = real_fopen(_dev->fname, "r+");
      if (!f) {
          return -EIO;
      }
      real_fseek(f, addr, SEEK_SET);
      for (size_t i = 0; i < size; i++) {
          uint8_t c = real_fgetc(f);
          real_fseek(f, -1, SEEK_CUR);
          real_fputc(c & ((uint8_t*)buff)[i], f);
      }
      real_fclose(f);
  
      return size;
  }
  
  static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size)
  {
      mtd_native_dev_t *_dev = (mtd_native_dev_t*) dev;
      size_t mtd_size = dev->sector_count * dev->pages_per_sector * dev->page_size;
      size_t sector_size = dev->pages_per_sector * dev->page_size;
  
      DEBUG("mtd_native: erase from sector %" PRIu32 " count %" PRIu32 "\n", addr, size);
  
      if (addr + size > mtd_size) {
          return -EOVERFLOW;
      }
      if (((addr % sector_size) != 0) || ((size % sector_size) != 0)) {
          return -EOVERFLOW;
      }
  
      FILE *f = real_fopen(_dev->fname, "r+");
      if (!f) {
          return -EIO;
      }
      real_fseek(f, addr, SEEK_SET);
      for (size_t i = 0; i < size; i++) {
          real_fputc(0xff, f);
      }
      real_fclose(f);
  
      return 0;
  }
  
  static int _power(mtd_dev_t *dev, enum mtd_power_state power)
  {
      (void) dev;
      (void) power;
  
      return -ENOTSUP;
  }
  
  
  const mtd_desc_t native_flash_driver = {
      .read = _read,
      .power = _power,
      .write = _write,
      .erase = _erase,
      .init = _init,
  };
  
  /** @} */