1 | #include "config.h" |
---|
2 | #include <ctype.h> |
---|
3 | #include "genlib.h" |
---|
4 | #include "stddefs.h" |
---|
5 | |
---|
6 | /* s_memcpy(): |
---|
7 | * Superset of memcpy(). Note, this used to be tfsmemcpy() in tfs.c; |
---|
8 | * however, since it has no real TFS dependencies, and code outside of |
---|
9 | * TFS uses it, it has been moved here and the name is changed. |
---|
10 | * |
---|
11 | * Includes verbose option plus verification after copy. |
---|
12 | * Takes advantage of address alignment when possible. |
---|
13 | * |
---|
14 | * Note: |
---|
15 | * If verbose is greater than one, then this function doesn't |
---|
16 | * do any memory transfer, it simply displays the memory range |
---|
17 | * that is to be transferred. This is used by TFS to dump a map without |
---|
18 | * actually touching memory. |
---|
19 | * |
---|
20 | * Return: |
---|
21 | * 0 if successful, else -1 indicating some part of the copy failed. |
---|
22 | */ |
---|
23 | int |
---|
24 | s_memcpy(char *_to,char *_from,int count, int verbose,int verifyonly) |
---|
25 | { |
---|
26 | int err; |
---|
27 | volatile register char *to, *from, *end; |
---|
28 | |
---|
29 | to = _to; |
---|
30 | from = _from; |
---|
31 | |
---|
32 | if (verbose) |
---|
33 | printf("%s %7d bytes from 0x%08lx to 0x%08lx", |
---|
34 | verifyonly ? "vrfy" : "copy", count,(ulong)from,(ulong)to); |
---|
35 | |
---|
36 | if (_to == _from) { |
---|
37 | if (verbose) |
---|
38 | printf("\n"); |
---|
39 | return(0); |
---|
40 | } |
---|
41 | |
---|
42 | if (count < 0) |
---|
43 | return(-1); |
---|
44 | |
---|
45 | if (verifyonly) { |
---|
46 | while(count) { |
---|
47 | if (*to != *from) |
---|
48 | break; |
---|
49 | to++; |
---|
50 | from++; |
---|
51 | count--; |
---|
52 | #ifdef WATCHDOG_ENABLED |
---|
53 | if ((count & 0xff) == 0) |
---|
54 | WATCHDOG_MACRO; |
---|
55 | #endif |
---|
56 | } |
---|
57 | if (count) { |
---|
58 | if (verbose) { |
---|
59 | printf(" FAILED\n"); |
---|
60 | printf(" (0x%02x @ 0x%08lx should be 0x%02x)\n", |
---|
61 | *to,(ulong)to,*from); |
---|
62 | } |
---|
63 | return(-1); |
---|
64 | } |
---|
65 | else |
---|
66 | if (verbose) |
---|
67 | printf(" OK\n"); |
---|
68 | return(0); |
---|
69 | } |
---|
70 | |
---|
71 | /* If verbose is greater than 1, then we don't even do a memcpy, |
---|
72 | * we just let the user know what we would have done... |
---|
73 | */ |
---|
74 | if ((count == 0) || (verbose > 1)) |
---|
75 | goto done; |
---|
76 | |
---|
77 | if (to != from) { |
---|
78 | |
---|
79 | err = 0; |
---|
80 | if (!((int)to & 3) && !((int)from & 3) && !(count & 3)) { |
---|
81 | volatile register ulong *lto, *lfrom, *lend; |
---|
82 | |
---|
83 | count >>= 2; |
---|
84 | lto = (ulong *)to; |
---|
85 | lfrom = (ulong *)from; |
---|
86 | lend = lto + count; |
---|
87 | while(lto < lend) { |
---|
88 | *lto = *lfrom; |
---|
89 | if (*lto != *lfrom) { |
---|
90 | err = 1; |
---|
91 | break; |
---|
92 | } |
---|
93 | lto++; |
---|
94 | lfrom++; |
---|
95 | #ifdef WATCHDOG_ENABLED |
---|
96 | if (((int)lto & 0xff) == 0) |
---|
97 | WATCHDOG_MACRO; |
---|
98 | #endif |
---|
99 | } |
---|
100 | } |
---|
101 | else if (!((int)to & 1) && !((int)from & 1) && !(count & 1)) { |
---|
102 | volatile register ushort *sto, *sfrom, *send; |
---|
103 | |
---|
104 | count >>= 1; |
---|
105 | sto = (ushort *)to; |
---|
106 | sfrom = (ushort *)from; |
---|
107 | send = sto + count; |
---|
108 | while(sto < send) { |
---|
109 | *sto = *sfrom; |
---|
110 | if (*sto != *sfrom) { |
---|
111 | err = 1; |
---|
112 | break; |
---|
113 | } |
---|
114 | sto++; |
---|
115 | sfrom++; |
---|
116 | #ifdef WATCHDOG_ENABLED |
---|
117 | if (((int)sto & 0xff) == 0) |
---|
118 | WATCHDOG_MACRO; |
---|
119 | #endif |
---|
120 | } |
---|
121 | } |
---|
122 | else { |
---|
123 | end = to + count; |
---|
124 | while(to < end) { |
---|
125 | *to = *from; |
---|
126 | if (*to != *from) { |
---|
127 | err = 1; |
---|
128 | break; |
---|
129 | } |
---|
130 | to++; |
---|
131 | from++; |
---|
132 | #ifdef WATCHDOG_ENABLED |
---|
133 | if (((int)to & 0xff) == 0) |
---|
134 | WATCHDOG_MACRO; |
---|
135 | #endif |
---|
136 | } |
---|
137 | } |
---|
138 | if (err) { |
---|
139 | if (verbose) |
---|
140 | printf(" failed\n"); |
---|
141 | return(-1); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | done: |
---|
146 | if (verbose) |
---|
147 | printf("\n"); |
---|
148 | |
---|
149 | return(0); |
---|
150 | } |
---|