1 /* $NetBSD: mem.c,v 1.4 2007/02/22 05:31:54 thorpej Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 #include <lib/libsa/stand.h>
34 #include <lib/libkern/libkern.h>
36 #include <machine/param.h>
37 #include <machine/bfs.h>
44 * >> mem g 0xf7e00000 4 4 0x8000 garom.bin
47 void mem_write(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
48 void mem_read(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
52 cmd_mem(int argc
, char *argp
[], int interactive
)
54 const char *filename
= 0;
64 for (i
= 0; i
< 4; i
++)
65 a
[i
] = strtoul(argp
[2 + i
], 0, 0);
69 size
= a
[3]; /* GA-ROM special */
77 if ((c
== 'd') || (c
== 'b') || (c
== 'g')) {
81 if (strlen(filename
) > BFS_FILENAME_MAXLEN
) {
82 printf("too long filename. (max %d)\n",
86 if ((p
= alloc(size
)) == 0) {
87 printf("can't allocate buffer.\n");
91 if (bfs_init(&bfs
) != 0) {
92 printf("no BFS partition.\n");
102 mem_read(a
[0], a
[1], a
[2], a
[3], 0);
108 v
= strtoul(argp
[6], 0, 0);
109 mem_write(a
[0], a
[1], a
[2], a
[3], (uint32_t)&v
);
113 mem_read(a
[0], a
[1], a
[2], a
[3], (uint32_t)p
);
114 if (bfs_file_write(bfs
, filename
, p
, size
) != 0)
115 printf("BFS write failed.\n");
120 if (bfs_file_read(bfs
, filename
, p
, size
, 0) != 0)
121 printf("BFS read failed.\n");
123 mem_write(a
[0], a
[1], a
[2], a
[3], (uint32_t)p
);
127 case 'g': /* GA-ROM special */
128 mem_read(a
[0], a
[1], a
[2], a
[3], (uint32_t)p
);
129 if (bfs_file_write(bfs
, filename
, p
, size
) != 0)
130 printf("BFS write failed.\n");
138 printf("mem r addr access_byte stride count\n");
139 printf("mem w addr access_byte stride count value\n");
140 printf("mem d addr access_byte stride count filename\n");
141 printf("mem b addr access_byte stride count filename\n");
142 printf("mem g addr access_byte stride count filename (GA-ROM only)\n");
147 mem_write(uint32_t dst_addr
, uint32_t access
, uint32_t stride
,
148 uint32_t count
, uint32_t src_addr
)
152 printf("write: addr=%p access=%dbyte stride=%dbyte count=%d Y/N?",
153 (void *)dst_addr
, access
, stride
, count
);
155 if (!prompt_yesno(1))
160 printf("invalid %dbyte access.\n", access
);
164 printf("%p = 0x%x\n",
165 (uint8_t *)dst_addr
, *(uint8_t *)src_addr
);
166 for (i
= 0; i
< count
; i
++,
167 dst_addr
+= stride
, src_addr
+= stride
) {
168 *(uint8_t *)dst_addr
= *(uint8_t *)src_addr
;
172 printf("%p = 0x%x\n",
173 (uint16_t *)dst_addr
, *(uint16_t *)src_addr
);
174 for (i
= 0; i
< count
; i
++,
175 dst_addr
+= stride
, src_addr
+= stride
) {
176 *(uint16_t *)dst_addr
= *(uint16_t *)src_addr
;
180 printf("%p = 0x%x\n",
181 (uint32_t *)dst_addr
, *(uint32_t *)src_addr
);
182 for (i
= 0; i
< count
; i
++,
183 dst_addr
+= stride
, src_addr
+= stride
) {
184 *(uint32_t *)dst_addr
= *(uint32_t *)src_addr
;
190 mem_read(uint32_t src_addr
, uint32_t access
, uint32_t stride
,
191 uint32_t count
, uint32_t dst_addr
)
196 printf("read: addr=%p access=%dbyte stride=%dbyte count=%d. Y/N?\n",
197 (void *)src_addr
, access
, stride
, count
);
199 if (!prompt_yesno(1))
203 for (i
= 0; i
< count
; i
++) {
206 printf("invalid %dbyte access.\n", access
);
209 v
= *(uint8_t *)src_addr
;
212 v
= *(uint16_t *)src_addr
;
215 v
= *(uint32_t *)src_addr
;
218 printf("%p: > 0x%x\n", (void *)src_addr
, v
);
224 printf("invalid %dbyte access.\n", access
);
227 for (i
= 0; i
< count
; i
++,
228 src_addr
+= stride
, dst_addr
+= stride
)
229 *(uint8_t *)dst_addr
= *(uint8_t *)src_addr
;
232 for (i
= 0; i
< count
; i
++,
233 src_addr
+= stride
, dst_addr
+= stride
)
234 *(uint16_t *)dst_addr
= *(uint16_t *)src_addr
;
238 for (i
= 0; i
< count
; i
++,
239 src_addr
+= 4, dst_addr
+= 1)
240 *(uint8_t *)dst_addr
=
241 *(uint32_t *)src_addr
;
243 for (i
= 0; i
< count
; i
++,
244 src_addr
+= stride
, dst_addr
+= stride
)
245 *(uint32_t *)dst_addr
=
246 *(uint32_t *)src_addr
;