1int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
2{
3 unsigned int block, lastblock;
4 unsigned int page, page_offset;
5
6
7 block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
8 lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
9 page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
10 page_offset = offs % CONFIG_SYS_NAND_PAGE_SIZE;
11
12 while (block <= lastblock) {
13 if (!nand_is_bad_block(block)) {
14
15 while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
16 nand_read_page(block, page, dst);
17
18
19
20
21
22
23 if (unlikely(page_offset)) {
24 memmove(dst, dst + page_offset,
25 CONFIG_SYS_NAND_PAGE_SIZE);
26 dst = (void *)((int)dst - page_offset);
27 page_offset = 0;
28 }
29 dst += CONFIG_SYS_NAND_PAGE_SIZE;
30 page++;
31 }
32
33 page = 0;
34 } else {
35 lastblock++;
36 }
37
38 block++;
39 }
40
41 return 0;
42}
43
44
45
46
47
48
49
50
51
52
53u32 nand_spl_adjust_offset(u32 sector, u32 offs)
54{
55 unsigned int block, lastblock;
56
57 block = sector / CONFIG_SYS_NAND_BLOCK_SIZE;
58 lastblock = (sector + offs) / CONFIG_SYS_NAND_BLOCK_SIZE;
59
60 while (block <= lastblock) {
61 if (nand_is_bad_block(block)) {
62 offs += CONFIG_SYS_NAND_BLOCK_SIZE;
63 lastblock++;
64 }
65
66 block++;
67 }
68
69 return offs;
70}
71
72#ifdef CONFIG_SPL_UBI
73
74
75
76
77
78
79static u8 scratch_buf[CONFIG_SYS_NAND_PAGE_SIZE];
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102int nand_spl_read_block(int block, int offset, int len, void *dst)
103{
104 int page, read;
105
106
107 page = offset / CONFIG_SYS_NAND_PAGE_SIZE;
108
109
110 offset = offset % CONFIG_SYS_NAND_PAGE_SIZE;
111
112 while (len) {
113
114
115
116
117 if (offset || len < CONFIG_SYS_NAND_PAGE_SIZE) {
118 nand_read_page(block, page, scratch_buf);
119 read = min(len, CONFIG_SYS_NAND_PAGE_SIZE - offset);
120 memcpy(dst, scratch_buf + offset, read);
121 offset = 0;
122 } else {
123 nand_read_page(block, page, dst);
124 read = CONFIG_SYS_NAND_PAGE_SIZE;
125 }
126 page++;
127 len -= read;
128 dst += read;
129 }
130 return 0;
131}
132#endif
133