annotate packages/redboot/current/src/mcopy.c @ 1725:e0ebd07e25f3

* cdl/redboot.cdl: Change CYGNUM_REDBOOT_FLASH_BASE to a booldata so we don't always look at address 0 for the flash!
author asl
date Tue, 14 Sep 2004 14:02:47 +0000
parents c4355f101bf6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1087
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
1 //==========================================================================
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
2 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
3 // mcopy.c
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
4 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
5 // RedBoot memory copy (mcopy) routine
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
6 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
7 //==========================================================================
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
8 //####ECOSGPLCOPYRIGHTBEGIN####
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
9 // -------------------------------------------
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
10 // This file is part of eCos, the Embedded Configurable Operating System.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
12 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
13 // eCos is free software; you can redistribute it and/or modify it under
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
14 // the terms of the GNU General Public License as published by the Free
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
15 // Software Foundation; either version 2 or (at your option) any later version.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
16 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
20 // for more details.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
21 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
22 // You should have received a copy of the GNU General Public License along
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
25 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
26 // As a special exception, if other files instantiate templates or use macros
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
27 // or inline functions from this file, or you compile this file and link it
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
28 // with other works to produce a work based on this file, this file does not
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
29 // by itself cause the resulting work to be covered by the GNU General Public
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
30 // License. However the source code for this file must still be made available
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
31 // in accordance with section (3) of the GNU General Public License.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
32 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
33 // This exception does not invalidate any other reasons why a work based on
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
34 // this file might be covered by the GNU General Public License.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
35 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
37 // at http://sources.redhat.com/ecos/ecos-license/
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
38 // -------------------------------------------
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
39 //####ECOSGPLCOPYRIGHTEND####
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
40 //==========================================================================
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
41 //#####DESCRIPTIONBEGIN####
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
42 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
43 // Author(s): msalter
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
44 // Contributors: msalter
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
45 // Date: 2003-07-01
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
46 // Purpose:
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
47 // Description:
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
48 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
49 // This code is part of RedBoot (tm).
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
50 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
51 //####DESCRIPTIONEND####
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
52 //
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
53 //==========================================================================
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
54
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
55 #include <redboot.h>
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
56
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
57 static void
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
58 do_mcopy(int argc, char *argv[])
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
59 {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
60 struct option_info opts[6];
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
61 unsigned long src, dst, len, end;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
62 bool src_set, dst_set, len_set;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
63 bool sz32, sz16, sz8;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
64 int incr = 1;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
65
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
66 init_opts(&opts[0], 's', true, OPTION_ARG_TYPE_NUM,
1513
c4355f101bf6 Cleanups to remove warnings
gthomas
parents: 1087
diff changeset
67 (void *)&src, (bool *)&src_set, "base address");
1087
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
68 init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM,
1513
c4355f101bf6 Cleanups to remove warnings
gthomas
parents: 1087
diff changeset
69 (void *)&len, (bool *)&len_set, "length");
1087
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
70 init_opts(&opts[2], 'd', true, OPTION_ARG_TYPE_NUM,
1513
c4355f101bf6 Cleanups to remove warnings
gthomas
parents: 1087
diff changeset
71 (void *)&dst, (bool *)&dst_set, "base address");
1087
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
72 init_opts(&opts[3], '4', false, OPTION_ARG_TYPE_FLG,
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
73 (void *)&sz32, (bool *)0, "copy 32 bit data");
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
74 init_opts(&opts[4], '2', false, OPTION_ARG_TYPE_FLG,
1513
c4355f101bf6 Cleanups to remove warnings
gthomas
parents: 1087
diff changeset
75 (void *)&sz16, (bool *)0, "copy 16 bit data");
1087
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
76 init_opts(&opts[5], '1', false, OPTION_ARG_TYPE_FLG,
1513
c4355f101bf6 Cleanups to remove warnings
gthomas
parents: 1087
diff changeset
77 (void *)&sz8, (bool *)0, "copy 8 bit data");
1087
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
78 if (!scan_opts(argc, argv, 1, opts, 6, 0, 0, "")) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
79 return;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
80 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
81
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
82 // Must have src, dst, len. No more than one size specifier.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
83 if (!src_set || !dst_set || !len_set || (sz32 + sz16 + sz8) > 1) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
84 diag_printf("usage: mcopy -s <addr> -d <addr> -l <length> [-1|-2|-4]\n");
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
85 return;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
86 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
87
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
88 // adjust incr and len for data size
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
89 if (sz16) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
90 len = (len + 1) & ~1;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
91 incr = 2;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
92 } else if (sz32 || !sz8) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
93 len = (len + 3) & ~3;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
94 incr = 4;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
95 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
96
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
97 end = src + len;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
98
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
99 // If overlapping areas, must copy backwards.
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
100 if (dst > src && dst < (src + len)) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
101 end = src - incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
102 src += (len - incr);
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
103 dst += (len - incr);
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
104 incr = -incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
105 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
106
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
107 if (sz8) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
108 while (src != end) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
109 *(cyg_uint8 *)dst = *(cyg_uint8 *)src;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
110 src += incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
111 dst += incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
112 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
113 } else if (sz16) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
114 while (src != end) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
115 *(cyg_uint16 *)dst = *(cyg_uint16 *)src;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
116 src += incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
117 dst += incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
118 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
119 } else {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
120 // Default - 32 bits
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
121 while (src != end) {
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
122 *(cyg_uint32 *)dst = *(cyg_uint32 *)src;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
123 src += incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
124 dst += incr;
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
125 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
126 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
127 }
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
128
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
129
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
130 RedBoot_cmd("mcopy",
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
131 "Copy memory from one address to another",
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
132 "-s <location> -d <location> -l <length> [-1|-2|-4]",
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
133 do_mcopy
549ccefb35ed Add RedBoot mcopy command
msalter
parents:
diff changeset
134 );