-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathnand.c
115 lines (97 loc) · 2.75 KB
/
nand.c
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
/*
* ibex - nand-related stuff
*
* Copyright (c) 2015 xerub
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "plib.h"
#include "nand.h"
#define LLB_PAGE_SIZE 1536
int
read_llb(h2fmi_read_boot_page_t read_page, int offset, int size, unsigned char *buf)
{
int rv = 0;
int page = div(offset, LLB_PAGE_SIZE); /*offset / LLB_PAGE_SIZE*/
int start = offset - page * LLB_PAGE_SIZE; /*offset % LLB_PAGE_SIZE*/
#if 0
unsigned char *tmp = malloc_(4096);
if (!tmp) {
return -1;
}
for (page += 2; size > 0; page++) {
int chunk = size;
if (chunk > LLB_PAGE_SIZE - start) {
chunk = LLB_PAGE_SIZE - start;
}
rv = read_page(0, page, tmp);
if (rv) {
break;
}
memcpy(buf, tmp + start, chunk);
start = 0;
buf += chunk;
size -= chunk;
}
#else /* XXX more efficient, but should be used ONLY if read_page reads exactly LLB_PAGE_SIZE bytes */
unsigned char *tmp = NULL;
page += 2;
if (start) {
int chunk = LLB_PAGE_SIZE - start;
if (!(tmp = malloc_(LLB_PAGE_SIZE))) return -1;
rv = read_page(0, page, tmp);
if (rv) {
goto err;
}
memcpy(buf, tmp + start, chunk);
buf += chunk;
size -= chunk;
page++;
}
for (; size >= LLB_PAGE_SIZE; page++) {
rv = read_page(0, page, buf);
if (rv) {
goto err;
}
buf += LLB_PAGE_SIZE;
size -= LLB_PAGE_SIZE;
}
if (size) {
if (!tmp && !(tmp = malloc_(LLB_PAGE_SIZE))) return -1;
rv = read_page(0, page, tmp);
if (rv == 0) {
memcpy(buf, tmp, size);
}
}
err:
#endif
free_(tmp);
return rv;
}
int
bdev_read(void *bdev, void *buf, long long offset, long long size)
{
if (version < 1940) {
return ((struct bdev6_t *)bdev)->bdev_read(bdev, buf, offset, size);
}
return ((struct bdev_t *)bdev)->bdev_read(bdev, buf, offset, size);
}
const char *
bdev_name(void *bdev)
{
if (version < 1940) {
return ((struct bdev6_t *)bdev)->name;
}
return ((struct bdev_t *)bdev)->name;
}