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
|
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2013 Daniel Gröber <dxld AT darkboxed DOT org>
* Copyright (C) 2013 Nuand LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <libbladeRF.h>
#include "rel_assert.h"
#include "log.h"
#include "spi_flash.h"
#include "board/board.h"
static inline int check_eb_access(struct bladerf *dev,
uint32_t erase_block, uint32_t count)
{
if (erase_block >= dev->flash_arch->num_ebs) {
log_debug("Invalid erase block: %u\n", erase_block);
return BLADERF_ERR_INVAL;
} else if (count > dev->flash_arch->num_ebs) {
log_debug("Invalid number of erase blocks: %u\n", count);
return BLADERF_ERR_INVAL;
} else if ((erase_block + count) > dev->flash_arch->num_ebs) {
log_debug("Requested operation extends past end of flash: "
"eb=%u, count=%u\n", erase_block, count);
return BLADERF_ERR_INVAL;
} else {
return 0;
}
}
static inline int check_page_access(struct bladerf *dev,
uint32_t page, uint32_t count)
{
if (page >= dev->flash_arch->num_pages) {
log_debug("Invalid page: %u\n", page);
return BLADERF_ERR_INVAL;
} else if (count > dev->flash_arch->num_pages) {
log_debug("Invalid number of pages: %u\n", count);
return BLADERF_ERR_INVAL;
} else if ((page + count) > dev->flash_arch->num_pages) {
log_debug("Requested operation extends past end of flash: "
"page=%u, count=%u\n", page, count);
return BLADERF_ERR_INVAL;
} else {
return 0;
}
}
int spi_flash_erase(struct bladerf *dev, uint32_t erase_block, uint32_t count)
{
int status = check_eb_access(dev, erase_block, count);
if (status == 0) {
status = dev->backend->erase_flash_blocks(dev, erase_block, count);
}
return status;
}
int spi_flash_read(struct bladerf *dev, uint8_t *buf,
uint32_t page, uint32_t count)
{
int status = check_page_access(dev, page, count);
if (status == 0) {
status = dev->backend->read_flash_pages(dev, buf, page, count);
}
return status;;
}
int spi_flash_write(struct bladerf *dev, const uint8_t *buf,
uint32_t page, uint32_t count)
{
int status = check_page_access(dev, page, count);
if (status == 0) {
status = dev->backend->write_flash_pages(dev, buf, page, count);
}
return status;
}
int spi_flash_verify(struct bladerf *dev, uint8_t *readback_buf,
const uint8_t *expected_buf, uint32_t page,
uint32_t count)
{
int status = 0;
size_t i;
const size_t len = count * dev->flash_arch->psize_bytes;
log_info("Verifying %u pages, starting at page %u\n", count, page);
status = spi_flash_read(dev, readback_buf, page, count);
if (status < 0) {
log_debug("Failed to read from flash: %s\n", bladerf_strerror(status));
return status;
}
for (i = 0; i < len; i++) {
if (expected_buf[i] != readback_buf[i]) {
status = BLADERF_ERR_UNEXPECTED;
log_info("Flash verification failed at byte %llu. "
"Read %02x, expected %02x\n",
i, readback_buf[i], expected_buf[i]);
break;
}
}
return status;
}
|