readmem.c
1 /*
2 * Copyright (C) 2002 ETC s.r.o.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 *
19 * Written by Marcel Telka <marcel@telka.sk>, 2002.
20 *
21 * Documentation:
22 * [1] JEDEC Solid State Technology Association, "Common Flash Interface (CFI)",
23 * September 1999, Order Number: JESD68
24 * [2] JEDEC Solid State Technology Association, "Common Flash Interface (CFI) ID Codes",
25 * September 2001, Order Number: JEP137-A
26 *
27 */
28
29 #include <sysdep.h>
30
31 #include <stdint.h>
32 #include <string.h>
33
34 #include <urjtag/log.h>
35 #include <urjtag/error.h>
36 #include <urjtag/bus.h>
37 #include <urjtag/flash.h>
38 #include <urjtag/jtag.h>
39
40 int
urj_bus_readmem(urj_bus_t * bus,FILE * f,uint32_t addr,uint32_t len)41 urj_bus_readmem (urj_bus_t *bus, FILE *f, uint32_t addr, uint32_t len)
42 {
43 uint32_t step;
44 uint64_t a;
45 size_t bc = 0;
46 #define BSIZE 4096
47 uint8_t b[BSIZE];
48 urj_bus_area_t area;
49 uint64_t end;
50
51 if (!bus)
52 {
53 urj_error_set (URJ_ERROR_NO_BUS_DRIVER, _("Missing bus driver"));
54 return URJ_STATUS_FAIL;
55 }
56
57 URJ_BUS_PREPARE (bus);
58
59 if (URJ_BUS_AREA (bus, addr, &area) != URJ_STATUS_OK)
60 return URJ_STATUS_FAIL;
61
62 step = area.width / 8;
63
64 if (step == 0)
65 {
66 urj_error_set (URJ_ERROR_INVALID, _("Unknown bus width"));
67 return URJ_STATUS_FAIL;
68 }
69 if (BSIZE % step != 0)
70 {
71 urj_error_set (URJ_ERROR_INVALID, "step %lu must divide BSIZE %d",
72 (long unsigned) step, BSIZE);
73 return URJ_STATUS_FAIL;
74 }
75
76 addr = addr & (~(step - 1));
77 len = (len + step - 1) & (~(step - 1));
78
79 urj_log (URJ_LOG_LEVEL_NORMAL, _("address: 0x%08lX\n"),
80 (long unsigned) addr);
81 urj_log (URJ_LOG_LEVEL_NORMAL, _("length: 0x%08lX\n"),
82 (long unsigned) len);
83
84 if (len == 0)
85 {
86 urj_error_set (URJ_ERROR_INVALID, _("length is 0"));
87 return URJ_STATUS_FAIL;
88 }
89
90 a = addr;
91 end = a + len;
92 urj_log (URJ_LOG_LEVEL_NORMAL, _("reading:\n"));
93
94 if (URJ_BUS_READ_START (bus, addr) != URJ_STATUS_OK)
95 return URJ_STATUS_FAIL;
96
97 for (a += step; a <= end; a += step)
98 {
99 uint32_t data;
100 int j;
101
102 if (a < end)
103 data = URJ_BUS_READ_NEXT (bus, a);
104 else
105 data = URJ_BUS_READ_END (bus);
106
107 for (j = step; j > 0; j--)
108 if (urj_get_file_endian () == URJ_ENDIAN_BIG)
109 b[bc++] = (data >> ((j - 1) * 8)) & 0xFF;
110 else
111 {
112 b[bc++] = data & 0xFF;
113 data >>= 8;
114 }
115
116 if ((bc >= BSIZE) || (a >= end))
117 {
118 urj_log (URJ_LOG_LEVEL_NORMAL, _("addr: 0x%08llX\r"),
119 (long long unsigned) a);
120 if (fwrite (b, bc, 1, f) != 1)
121 {
122 urj_error_set (URJ_ERROR_FILEIO, "fwrite fails");
123 urj_error_state.sys_errno = ferror(f);
124 clearerr(f);
125 return URJ_STATUS_FAIL;
126 }
127 bc = 0;
128 }
129 }
130
131 urj_log (URJ_LOG_LEVEL_NORMAL, _("\nDone.\n"));
132
133 return URJ_STATUS_OK;
134 }
135