]> git.ipfire.org Git - people/ms/u-boot.git/blob - tools/updater/flash.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[people/ms/u-boot.git] / tools / updater / flash.c
1 /*
2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8 #include <common.h>
9 #include <flash.h>
10
11 extern flash_info_t flash_info[]; /* info for FLASH chips */
12
13 /*-----------------------------------------------------------------------
14 * Functions
15 */
16
17 /*-----------------------------------------------------------------------
18 * Set protection status for monitor sectors
19 *
20 * The monitor is always located in the _first_ Flash bank.
21 * If necessary you have to map the second bank at lower addresses.
22 */
23 void
24 flash_protect (int flag, ulong from, ulong to, flash_info_t *info)
25 {
26 ulong b_end = info->start[0] + info->size - 1; /* bank end address */
27 short s_end = info->sector_count - 1; /* index of last sector */
28 int i;
29
30 /* Do nothing if input data is bad. */
31 if (info->sector_count == 0 || info->size == 0 || to < from) {
32 return;
33 }
34
35 /* There is nothing to do if we have no data about the flash
36 * or the protect range and flash range don't overlap.
37 */
38 if (info->flash_id == FLASH_UNKNOWN ||
39 to < info->start[0] || from > b_end) {
40 return;
41 }
42
43 for (i=0; i<info->sector_count; ++i) {
44 ulong end; /* last address in current sect */
45
46 end = (i == s_end) ? b_end : info->start[i + 1] - 1;
47
48 /* Update protection if any part of the sector
49 * is in the specified range.
50 */
51 if (from <= end && to >= info->start[i]) {
52 if (flag & FLAG_PROTECT_CLEAR) {
53 #if defined(CONFIG_SYS_FLASH_PROTECTION)
54 flash_real_protect(info, i, 0);
55 #else
56 info->protect[i] = 0;
57 #endif /* CONFIG_SYS_FLASH_PROTECTION */
58 }
59 else if (flag & FLAG_PROTECT_SET) {
60 #if defined(CONFIG_SYS_FLASH_PROTECTION)
61 flash_real_protect(info, i, 1);
62 #else
63 info->protect[i] = 1;
64 #endif /* CONFIG_SYS_FLASH_PROTECTION */
65 }
66 }
67 }
68 }
69
70 /*-----------------------------------------------------------------------
71 */
72
73 flash_info_t *
74 addr2info (ulong addr)
75 {
76 #ifndef CONFIG_SPD823TS
77 flash_info_t *info;
78 int i;
79
80 for (i=0, info = &flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
81 if (info->flash_id != FLASH_UNKNOWN &&
82 addr >= info->start[0] &&
83 /* WARNING - The '- 1' is needed if the flash
84 * is at the end of the address space, since
85 * info->start[0] + info->size wraps back to 0.
86 * Please don't change this unless you understand this.
87 */
88 addr <= info->start[0] + info->size - 1) {
89 return (info);
90 }
91 }
92 #endif /* CONFIG_SPD823TS */
93
94 return (NULL);
95 }
96
97 /*-----------------------------------------------------------------------
98 * Copy memory to flash.
99 * Make sure all target addresses are within Flash bounds,
100 * and no protected sectors are hit.
101 * Returns:
102 * ERR_OK 0 - OK
103 * ERR_TIMOUT 1 - write timeout
104 * ERR_NOT_ERASED 2 - Flash not erased
105 * ERR_PROTECTED 4 - target range includes protected sectors
106 * ERR_INVAL 8 - target address not in Flash memory
107 * ERR_ALIGN 16 - target address not aligned on boundary
108 * (only some targets require alignment)
109 */
110 int
111 flash_write (char *src, ulong addr, ulong cnt)
112 {
113 #ifdef CONFIG_SPD823TS
114 return (ERR_TIMOUT); /* any other error codes are possible as well */
115 #else
116 int i;
117 ulong end = addr + cnt - 1;
118 flash_info_t *info_first = addr2info (addr);
119 flash_info_t *info_last = addr2info (end );
120 flash_info_t *info;
121 int j;
122
123 if (cnt == 0) {
124 return (ERR_OK);
125 }
126
127 if (!info_first || !info_last) {
128 return (ERR_INVAL);
129 }
130
131 for (info = info_first; info <= info_last; ++info) {
132 ulong b_end = info->start[0] + info->size; /* bank end addr */
133 short s_end = info->sector_count - 1;
134 for (i=0; i<info->sector_count; ++i) {
135 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
136
137 if ((end >= info->start[i]) && (addr < e_addr) &&
138 (info->protect[i] != 0) ) {
139 return (ERR_PROTECTED);
140 }
141 }
142 }
143
144 printf("\rWriting ");
145 for (j=0; j<20; j++) putc(177);
146 printf("\rWriting ");
147
148 /* finally write data to flash */
149 for (info = info_first; info <= info_last && cnt>0; ++info) {
150 ulong len;
151
152 len = info->start[0] + info->size - addr;
153 if (len > cnt)
154 len = cnt;
155
156 if ((i = write_buff(info, src, addr, len)) != 0) {
157 return (i);
158 }
159 cnt -= len;
160 addr += len;
161 src += len;
162 }
163 return (ERR_OK);
164 #endif /* CONFIG_SPD823TS */
165 }
166
167 /*-----------------------------------------------------------------------
168 */