Skip to content

Commit 023b287

Browse files
authored
Merge pull request arduino#71 from bcmi-labs/mbr_fix_main
MBRBlockDevice fixes and cleanup
2 parents a7c9a2f + b03dafe commit 023b287

31 files changed

+272
-105
lines changed

Diff for: libraries/BlockDevices/BlockDevice.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
BlockDevice.cpp
3+
Copyright (c) 2023 Arduino SA. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <QSPIFlashBlockDevice.h>
21+
22+
BlockDevice *BlockDevice::get_default_instance()
23+
{
24+
static QSPIFlashBlockDevice default_bd(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3);
25+
return &default_bd;
26+
}

Diff for: libraries/BlockDevices/blockDevice.h renamed to libraries/BlockDevices/BlockDevice.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* ########################################################################## */
2-
/* - File: blockDevice.h
2+
/* - File: BlockDevice.h
33
- Copyright (c): 2022 Arduino srl. All right reserved.
44
- Author: Daniele Aimo ([email protected])
55
@@ -23,6 +23,7 @@
2323
#define ARDUINO_BLOCK_DEVICE
2424
#include "Arduino.h"
2525
#include <stdint.h>
26+
#include "../Storage/storage_common.h"
2627

2728
#define BLOCK_DEVICE_OK (0)
2829
#define BD_ERROR_OK (0)
@@ -34,7 +35,7 @@ typedef uint32_t bd_size_t;
3435
typedef pin_size_t pin_t;
3536

3637
/* -------------------------------------------------------------------------- */
37-
/* Abstract Base blockDevice class (defines the INTERFACE for all the
38+
/* Abstract Base BlockDevice class (defines the INTERFACE for all the
3839
subclass) */
3940
/* -------------------------------------------------------------------------- */
4041

@@ -52,6 +53,8 @@ class BlockDevice {
5253
BlockDevice(BlockDevice const&) = delete;
5354
void operator=(BlockDevice const&) = delete;
5455

56+
static BlockDevice *get_default_instance();
57+
5558
virtual ~BlockDevice() = default;
5659
/* initialize a block device */
5760
virtual int init() = 0;

Diff for: libraries/BlockDevices/BufferedBlockDevice.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
#include "BufferedBlockDevice.h"
19-
#include "../Storage/storage_common.h"
2019
#include <algorithm>
2120
#include <string.h>
2221

Diff for: libraries/BlockDevices/BufferedBlockDevice.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#ifndef ARDUINO_BUFFERED_BLOCK_DEVICE_H
2222
#define ARDUINO_BUFFERED_BLOCK_DEVICE_H
2323

24-
#include "blockDevice.h"
24+
#include "BlockDevice.h"
2525

2626
//namespace mbed {
2727

Diff for: libraries/BlockDevices/ChainingBlockDevice.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include "ChainingBlockDevice.h"
1919

20-
2120
//namespace mbed {
2221

2322
ChainingBlockDevice::ChainingBlockDevice(BlockDevice **bds, size_t bd_count)

Diff for: libraries/BlockDevices/ChainingBlockDevice.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#ifndef ARDUINO_CHAINING_BLOCK_DEVICE_H
2222
#define ARDUINO_CHAINING_BLOCK_DEVICE_H
2323

24-
#include "blockDevice.h"
25-
#include "../Storage/storage_common.h"
24+
#include "BlockDevice.h"
2625
#include <stdlib.h>
2726

2827
//namespace mbed {

Diff for: libraries/BlockDevices/DataFlashBlockDevice.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//#define DATA_FLASH_DEBUG
2424

2525
/* base class for block devices */
26-
#include "blockDevice.h"
26+
#include "BlockDevice.h"
2727

2828
/* Arduino.h to include the defines of the flash type LP or HP*/
2929
#include "Arduino.h"

Diff for: libraries/BlockDevices/ExhaustibleBlockDevice.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
#include "ExhaustibleBlockDevice.h"
19-
#include "../Storage/storage_common.h"
2019

2120
//namespace mbed {
2221

Diff for: libraries/BlockDevices/ExhaustibleBlockDevice.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#ifndef ARDUINO_EXHAUSTIBLE_BLOCK_DEVICE_H
2222
#define ARDUINO_EXHAUSTIBLE_BLOCK_DEVICE_H
2323

24-
#include "blockDevice.h"
24+
#include "BlockDevice.h"
2525

2626
//namespace mbed {
2727

Diff for: libraries/BlockDevices/FlashSimBlockDevice.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
*/
1717

1818
#include "FlashSimBlockDevice.h"
19-
#include "../Storage/storage_common.h"
2019
#include <algorithm>
2120
#include <stdlib.h>
2221
#include <string.h>
2322

24-
2523
//namespace mbed {
2624

2725
static const bd_size_t min_blank_buf_size = 32;

Diff for: libraries/BlockDevices/FlashSimBlockDevice.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#ifndef ARDUINO_FLASH_SIM_BLOCK_DEVICE_H
2222
#define ARDUINO_FLASH_SIM_BLOCK_DEVICE_H
2323

24-
#include "blockDevice.h"
24+
#include "BlockDevice.h"
2525

2626
//namespace mbed {
2727

Diff for: libraries/BlockDevices/HeapBlockDevice.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#ifndef ARDUINO_MEM_BLOCK_DEVICE_H
2222
#define ARDUINO_MEM_BLOCK_DEVICE_H
2323

24-
#include "blockDevice.h"
25-
#include "../Storage/storage_common.h"
24+
#include "BlockDevice.h"
2625
#include <string.h>
2726
#include <stdlib.h>
2827

Diff for: libraries/BlockDevices/MBRBlockDevice.cpp

+29-18
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
*/
1717

1818
#include "MBRBlockDevice.h"
19-
#include "../Storage/storage_common.h"
2019
#include <algorithm>
2120
#include <string.h>
2221

23-
//#define DEBUG_MSD
24-
extern "C" int mylogadd(const char *fmt, ...) ;
22+
// To enable debug set MBR_DBG to 1 and make sure STORAGE_DEBUG is defined
23+
// in Storage/storage_common.h
24+
#define MBR_DBG 0
25+
#define MBR_MEM_DBG 0
26+
2527
//namespace mbed {
2628

2729
// On disk structures, all entries are little endian
@@ -90,6 +92,7 @@ static int partition_absolute(
9092
int err = bd->read(buffer, 512 - buffer_size, buffer_size);
9193
if (err) {
9294
delete[] buffer;
95+
debug_if(MBR_DBG, "MBR partition_absolute: read error %d", err);
9396
return err;
9497
}
9598

@@ -159,11 +162,15 @@ static int partition_absolute(
159162
err = bd->erase(0, bd->get_erase_size());
160163
if (err) {
161164
delete[] buffer;
165+
debug_if(MBR_DBG, "MBR partition_absolute: erase error %d", err);
162166
return err;
163167
}
164168

165169
err = bd->program(buffer, 512 - buffer_size, buffer_size);
166170
delete[] buffer;
171+
if(err) {
172+
debug_if(MBR_DBG, "MBR partition_absolute: program error %d", err);
173+
}
167174
return err;
168175
}
169176

@@ -202,6 +209,7 @@ int MBRBlockDevice::partition(BlockDevice *bd, int part, uint8_t type,
202209
{
203210
int err = bd->init();
204211
if (err) {
212+
debug_if(MBR_DBG, "MBR partition: init error %d", err);
205213
return err;
206214
}
207215

@@ -217,11 +225,13 @@ int MBRBlockDevice::partition(BlockDevice *bd, int part, uint8_t type,
217225

218226
err = partition_absolute(bd, part, type, offset, size);
219227
if (err) {
228+
debug_if(MBR_DBG, "MBR partition: partition_absolute error %d", err);
220229
return err;
221230
}
222231

223232
err = bd->deinit();
224233
if (err) {
234+
debug_if(MBR_DBG, "MBR partition: bd deinit error %d", err);
225235
return err;
226236
}
227237

@@ -245,11 +255,13 @@ int MBRBlockDevice::init()
245255
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
246256

247257
if (val != 1) {
258+
debug_if(MBR_DBG, "MBR init: atomic increment error %d", val);
248259
return BD_ERROR_OK;
249260
}
250261

251262
err = _bd->init();
252263
if (err) {
264+
debug_if(MBR_DBG, "MBR init: bd init error %d", err);
253265
goto fail;
254266
}
255267

@@ -259,13 +271,19 @@ int MBRBlockDevice::init()
259271

260272
err = _bd->read(buffer, 512 - buffer_size, buffer_size);
261273
if (err) {
274+
debug_if(MBR_DBG, "MBR init: read error %d", err);
262275
goto fail;
263276
}
264277

278+
#if MBR_MEM_DBG
279+
debug_mem(buffer, buffer_size);
280+
#endif
281+
265282
// Check for valid table
266283
table = reinterpret_cast<struct mbr_table *>(&buffer[buffer_size - sizeof(struct mbr_table)]);
267284
if (table->signature[0] != 0x55 || table->signature[1] != 0xaa) {
268285
err = BD_ERROR_INVALID_MBR;
286+
debug_if(MBR_DBG, "MBR init: table error 0x%02x 0x%02x", table->signature[0], table->signature[1]);
269287
goto fail;
270288
}
271289

@@ -274,6 +292,7 @@ int MBRBlockDevice::init()
274292
if (table->entries[_part - 1].status != 0x00 &&
275293
table->entries[_part - 1].status != 0x80) {
276294
err = BD_ERROR_INVALID_PARTITION;
295+
debug_if(MBR_DBG, "MBR init: part status error");
277296
goto fail;
278297
}
279298

@@ -284,31 +303,27 @@ int MBRBlockDevice::init()
284303
table->entries[_part - 1].type == 0x05 ||
285304
table->entries[_part - 1].type == 0x0f)) {
286305
err = BD_ERROR_INVALID_PARTITION;
306+
debug_if(MBR_DBG, "MBR init: part entry error");
287307
goto fail;
288308
}
289309

290310
// Get partition attributes
291311
sector = std::max<uint32_t>(_bd->get_erase_size(), 512);
292-
#ifdef DEBUG_MSD
293-
mylogadd("MBR sectort %i", sector);
294-
#endif
312+
debug_if(MBR_DBG, "MBR sector %i", sector);
295313

296314
_type = table->entries[_part - 1].type;
297315
_offset = fromle32(table->entries[_part - 1].lba_offset) * sector;
298-
#ifdef DEBUG_MSD
299-
mylogadd("MBR _offset %i", _offset);
300-
#endif
316+
debug_if(MBR_DBG, "MBR _offset %i", _offset);
301317

302318
_size = fromle32(table->entries[_part - 1].lba_size) * sector;
303319

304-
#ifdef DEBUG_MSD
305-
mylogadd("MBR _size %i", _size);
306-
#endif
320+
debug_if(MBR_DBG, "MBR _size %i", _size);
307321

308322

309323
// Check that block addresses are valid
310324
if (!_bd->is_valid_erase(_offset, _size)) {
311325
err = BD_ERROR_INVALID_PARTITION;
326+
debug_if(MBR_DBG, "MBR init valid erase error");
312327
goto fail;
313328
}
314329

@@ -357,9 +372,7 @@ int MBRBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
357372
if (!is_valid_read(addr, size)) {
358373
return BD_ERROR_DEVICE_ERROR;
359374
}
360-
#ifdef DEBUG_MSD
361-
mylogadd("MBR READ %i, %i", addr + _offset, size);
362-
#endif
375+
debug_if(MBR_DBG, "MBR READ %i, %i", addr + _offset, size);
363376
return _bd->read(b, addr + _offset, size);
364377
}
365378

@@ -372,9 +385,7 @@ int MBRBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
372385
if (!is_valid_program(addr, size)) {
373386
return BD_ERROR_DEVICE_ERROR;
374387
}
375-
#ifdef DEBUG_MSD
376-
mylogadd("MBR WRITE %i, %i", addr + _offset, size);
377-
#endif
388+
debug_if(MBR_DBG, "MBR WRITE %i, %i", addr + _offset, size);
378389
return _bd->program(b, addr + _offset, size);
379390
}
380391

Diff for: libraries/BlockDevices/MBRBlockDevice.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#ifndef ARDUINO_MBR_BLOCK_DEVICE_H
2222
#define ARDUINO_MBR_BLOCK_DEVICE_H
2323

24-
#include "blockDevice.h"
24+
#include "BlockDevice.h"
2525

2626
//namespace mbed {
2727

Diff for: libraries/BlockDevices/ObservingBlockDevice.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
#ifndef ARDUINO_OBSERVING_BLOCK_DEVICE_H
2222
#define ARDUINO_OBSERVING_BLOCK_DEVICE_H
2323

24-
#include "blockDevice.h"
25-
#include "../Storage/storage_common.h"
26-
24+
#include "BlockDevice.h"
2725

2826
//namespace mbed {
2927

0 commit comments

Comments
 (0)