summaryrefslogtreecommitdiff
path: root/lib/x1000-installer/test_lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib/x1000-installer/test_lib')
-rw-r--r--lib/x1000-installer/test_lib/core_alloc.c72
-rw-r--r--lib/x1000-installer/test_lib/core_alloc.h43
-rw-r--r--lib/x1000-installer/test_lib/fakenand.c270
-rw-r--r--lib/x1000-installer/test_lib/file.c11
-rw-r--r--lib/x1000-installer/test_lib/file.h18
-rw-r--r--lib/x1000-installer/test_lib/md5.c245
-rw-r--r--lib/x1000-installer/test_lib/md5.h18
-rw-r--r--lib/x1000-installer/test_lib/nand-x1000.h112
-rw-r--r--lib/x1000-installer/test_lib/pathfuncs.c130
-rw-r--r--lib/x1000-installer/test_lib/pathfuncs.h39
-rw-r--r--lib/x1000-installer/test_lib/strlcpy.c50
-rw-r--r--lib/x1000-installer/test_lib/strlcpy.h4
-rw-r--r--lib/x1000-installer/test_lib/system.h10
13 files changed, 0 insertions, 1022 deletions
diff --git a/lib/x1000-installer/test_lib/core_alloc.c b/lib/x1000-installer/test_lib/core_alloc.c
deleted file mode 100644
index 719670f8f2..0000000000
--- a/lib/x1000-installer/test_lib/core_alloc.c
+++ /dev/null
@@ -1,72 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "core_alloc.h"
23#include <stdlib.h>
24
25#define N_POINTERS 100
26
27static void* pointers[N_POINTERS];
28struct buflib_callbacks buflib_ops_locked = {NULL, NULL, NULL};
29
30int core_alloc(const char* name, size_t size)
31{
32 (void)name;
33
34 void* mem = malloc(size);
35 if(!mem)
36 return -1;
37
38 for(int i = 0; i < N_POINTERS; ++i) {
39 if(pointers[i])
40 continue;
41
42 pointers[i] = mem;
43 return i + 1;
44 }
45
46 free(mem);
47 return -1;
48}
49
50int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks* cb)
51{
52 (void)cb;
53 return core_alloc(name, size);
54}
55
56int core_free(int handle)
57{
58 if(handle > 0) {
59 free(pointers[handle-1]);
60 pointers[handle-1] = NULL;
61 }
62
63 return 0;
64}
65
66void* core_get_data(int handle)
67{
68 if(handle > 0)
69 return pointers[handle-1];
70
71 return NULL;
72}
diff --git a/lib/x1000-installer/test_lib/core_alloc.h b/lib/x1000-installer/test_lib/core_alloc.h
deleted file mode 100644
index 2c77e3c274..0000000000
--- a/lib/x1000-installer/test_lib/core_alloc.h
+++ /dev/null
@@ -1,43 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22/* fake core_alloc implementation for testing */
23
24#ifndef CORE_ALLOC_H
25#define CORE_ALLOC_H
26
27#include <stddef.h>
28#include <stdbool.h>
29
30struct buflib_callbacks {
31 int (*move_callback)(int handle, void* current, void* new);
32 int (*shrink_callback)(int handle, unsigned hints, void* start, size_t old_size);
33 void (*sync_callback)(int handle, bool sync_on);
34};
35
36extern struct buflib_callbacks buflib_ops_locked;
37
38int core_alloc(const char* name, size_t size);
39int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks* cb);
40int core_free(int handle);
41void* core_get_data(int handle);
42
43#endif
diff --git a/lib/x1000-installer/test_lib/fakenand.c b/lib/x1000-installer/test_lib/fakenand.c
deleted file mode 100644
index 19f1f31cfd..0000000000
--- a/lib/x1000-installer/test_lib/fakenand.c
+++ /dev/null
@@ -1,270 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "nand-x1000.h"
23#include <unistd.h>
24#include <fcntl.h>
25#include <string.h>
26#include <stdio.h>
27#include <stdlib.h>
28
29const char* nand_backing_file = "fakeNAND.bin";
30const char* nand_meta_file = "fakeNAND_meta.bin";
31
32struct nand_trace* nand_trace = NULL;
33size_t nand_trace_capacity = 0;
34size_t nand_trace_length = 0;
35
36static struct nand_trace* nand_trace_cur = NULL;
37
38static int injected_err = 0;
39
40#define METAF_PROGRAMMED 1
41
42static const nand_chip fake_chip = {
43 /* ATO25D1GA */
44 .log2_ppb = 6, /* 64 pages */
45 .page_size = 2048,
46 .oob_size = 64,
47 .nr_blocks = 1024,
48};
49
50void nand_trace_reset(size_t size)
51{
52 nand_trace = realloc(nand_trace, size);
53 nand_trace_capacity = size;
54 nand_trace_length = 0;
55 nand_trace_cur = nand_trace;
56}
57
58void nand_inject_error(int rc)
59{
60 injected_err = rc;
61}
62
63nand_drv* nand_init(void)
64{
65 static bool inited = false;
66 static uint8_t scratch_buf[NAND_DRV_SCRATCHSIZE];
67 static uint8_t page_buf[NAND_DRV_MAXPAGESIZE];
68 static nand_drv d;
69
70 if(!inited) {
71 d.scratch_buf = scratch_buf;
72 d.page_buf = page_buf;
73 d.chip = &fake_chip;
74 inited = true;
75 }
76
77 return &d;
78}
79
80static void lock_assert(bool cond, const char* msg)
81{
82 if(!cond) {
83 fprintf(stderr, "%s\n", msg);
84 fflush(stderr);
85 abort();
86 }
87}
88
89void nand_lock(nand_drv* drv)
90{
91 drv->lock_count++;
92}
93
94void nand_unlock(nand_drv* drv)
95{
96 lock_assert(drv->lock_count > 0, "nand_unlock() called when not locked");
97 drv->lock_count--;
98}
99
100#define CHECK_INJECTED_ERROR \
101 do { int __err = injected_err; injected_err = 0; if(__err) return __err; } while(0)
102
103int nand_open(nand_drv* drv)
104{
105 lock_assert(drv->lock_count > 0, "nand_open(): lock not held");
106 CHECK_INJECTED_ERROR;
107
108 if(drv->refcount > 0) {
109 drv->refcount++;
110 return NAND_SUCCESS;
111 }
112
113 /* leaks an fd on error but this is only testing... */
114 drv->fd = open(nand_backing_file, O_RDWR|O_CREAT, 0644);
115 drv->metafd = open(nand_meta_file, O_RDWR|O_CREAT, 0644);
116 if(drv->fd < 0 || drv->metafd < 0)
117 goto err;
118
119 drv->ppb = 1 << drv->chip->log2_ppb;
120 drv->fpage_size = drv->chip->page_size + drv->chip->oob_size;
121
122 /* make backing file the correct size */
123 if(ftruncate(drv->fd, drv->chip->page_size * drv->ppb * drv->chip->nr_blocks) < 0)
124 goto err;
125 if(ftruncate(drv->metafd, drv->chip->nr_blocks * drv->ppb) < 0)
126 goto err;
127
128 drv->refcount++;
129 return NAND_SUCCESS;
130
131 err:
132 if(drv->fd >= 0)
133 close(drv->fd);
134 if(drv->metafd >= 0)
135 close(drv->metafd);
136 return NAND_ERR_OTHER;
137}
138
139void nand_close(nand_drv* drv)
140{
141 lock_assert(drv->lock_count > 0, "nand_close(): lock not held");
142
143 if(--drv->refcount > 0)
144 return;
145
146 close(drv->fd);
147 close(drv->metafd);
148 drv->fd = -1;
149 drv->metafd = -1;
150}
151
152static int read_meta(nand_drv* drv, nand_page_t page)
153{
154 /* probably won't fail */
155 if(lseek(drv->metafd, page, SEEK_SET) < 0)
156 return NAND_ERR_OTHER;
157 if(read(drv->metafd, drv->scratch_buf, 1) != 1)
158 return NAND_ERR_OTHER;
159
160 return drv->scratch_buf[0];
161}
162
163static int write_meta(nand_drv* drv, nand_page_t page, int val)
164{
165 drv->scratch_buf[0] = val;
166
167 if(lseek(drv->metafd, page, SEEK_SET) < 0)
168 return NAND_ERR_OTHER;
169 if(write(drv->metafd, drv->scratch_buf, 1) != 1)
170 return NAND_ERR_OTHER;
171
172 return NAND_SUCCESS;
173}
174
175static int upd_meta(nand_drv* drv, nand_page_t page, uint8_t clr, uint8_t set)
176{
177 int meta = read_meta(drv, page);
178 if(meta < 0)
179 return meta;
180
181 meta &= ~clr;
182 meta |= set;
183
184 return write_meta(drv, page, meta);
185}
186
187static int page_program(nand_drv* drv, nand_page_t page, const void* buffer,
188 uint8_t clr, uint8_t set)
189{
190 if(lseek(drv->fd, page * drv->chip->page_size, SEEK_SET) < 0)
191 return NAND_ERR_OTHER;
192 if(write(drv->fd, buffer, drv->chip->page_size) != (ssize_t)drv->chip->page_size)
193 return NAND_ERR_PROGRAM_FAIL;
194
195 return upd_meta(drv, page, clr, set);
196}
197
198static void trace(enum nand_trace_type ty, enum nand_trace_exception ex, nand_page_t addr)
199{
200 if(nand_trace_length < nand_trace_capacity) {
201 nand_trace_cur->type = ty;
202 nand_trace_cur->exception = ex;
203 nand_trace_cur->addr = addr;
204 nand_trace_cur++;
205 nand_trace_length++;
206 }
207}
208
209int nand_block_erase(nand_drv* drv, nand_block_t block)
210{
211 lock_assert(drv->lock_count > 0, "nand_block_erase(): lock not held");
212 CHECK_INJECTED_ERROR;
213
214 trace(NTT_ERASE, NTE_NONE, block);
215
216 memset(drv->page_buf, 0xff, drv->fpage_size);
217
218 for(unsigned i = 0; i < drv->ppb; ++i) {
219 int rc = page_program(drv, block + i, drv->page_buf, METAF_PROGRAMMED, 0);
220 if(rc < 0)
221 return NAND_ERR_ERASE_FAIL;
222 }
223
224 return NAND_SUCCESS;
225}
226
227int nand_page_program(nand_drv* drv, nand_page_t page, const void* buffer)
228{
229 lock_assert(drv->lock_count > 0, "nand_page_program(): lock not held");
230 CHECK_INJECTED_ERROR;
231
232 int meta = read_meta(drv, page);
233 if(meta < 0)
234 return meta;
235
236 enum nand_trace_exception exception = NTE_NONE;
237 if(meta & METAF_PROGRAMMED)
238 exception = NTE_DOUBLE_PROGRAMMED;
239
240 trace(NTT_PROGRAM, exception, page);
241
242 return page_program(drv, page, buffer, 0, METAF_PROGRAMMED);
243}
244
245int nand_page_read(nand_drv* drv, nand_page_t page, void* buffer)
246{
247 lock_assert(drv->lock_count > 0, "nand_page_read(): lock not held");
248 CHECK_INJECTED_ERROR;
249
250 enum nand_trace_exception exception = NTE_NONE;
251
252 int meta = read_meta(drv, page);
253 if(meta < 0)
254 return meta;
255
256 if(meta & METAF_PROGRAMMED) {
257 if(lseek(drv->fd, page * drv->chip->page_size, SEEK_SET) < 0)
258 return NAND_ERR_OTHER;
259 if(read(drv->fd, buffer, drv->chip->page_size) != (ssize_t)drv->chip->page_size)
260 return NAND_ERR_OTHER;
261 } else {
262 memset(buffer, 0xff, drv->chip->page_size);
263 exception = NTE_CLEARED;
264 }
265
266 trace(NTT_READ, exception, page);
267
268 memset(buffer + drv->chip->page_size, 0xff, drv->chip->oob_size);
269 return NAND_SUCCESS;
270}
diff --git a/lib/x1000-installer/test_lib/file.c b/lib/x1000-installer/test_lib/file.c
deleted file mode 100644
index 8769c009a4..0000000000
--- a/lib/x1000-installer/test_lib/file.c
+++ /dev/null
@@ -1,11 +0,0 @@
1#include "file.h"
2
3off_t filesize(int osfd)
4{
5 struct stat sb;
6
7 if (!fstat(osfd, &sb))
8 return sb.st_size;
9 else
10 return -1;
11}
diff --git a/lib/x1000-installer/test_lib/file.h b/lib/x1000-installer/test_lib/file.h
deleted file mode 100644
index 2a6554c695..0000000000
--- a/lib/x1000-installer/test_lib/file.h
+++ /dev/null
@@ -1,18 +0,0 @@
1#ifndef FILE_H
2#define FILE_H
3
4#include <sys/types.h>
5#include <sys/statfs.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <unistd.h>
9
10#ifdef MAX_PATH
11# undef MAX_PATH
12#endif
13
14#define MAX_PATH 260
15
16off_t filesize(int fd);
17
18#endif
diff --git a/lib/x1000-installer/test_lib/md5.c b/lib/x1000-installer/test_lib/md5.c
deleted file mode 100644
index 3050c7ebd8..0000000000
--- a/lib/x1000-installer/test_lib/md5.c
+++ /dev/null
@@ -1,245 +0,0 @@
1/*
2 * RFC 1321 compliant MD5 implementation
3 *
4 * Copyright (C) 2001-2003 Christophe Devine
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <string.h>
22
23#include "md5.h"
24
25#define GET_UINT32(n,b,i) \
26{ \
27 (n) = ( (uint32_t) (b)[(i) ] ) \
28 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
29 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
30 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
31}
32
33#define PUT_UINT32(n,b,i) \
34{ \
35 (b)[(i) ] = (uint8_t) ( (n) ); \
36 (b)[(i) + 1] = (uint8_t) ( (n) >> 8 ); \
37 (b)[(i) + 2] = (uint8_t) ( (n) >> 16 ); \
38 (b)[(i) + 3] = (uint8_t) ( (n) >> 24 ); \
39}
40
41void md5_starts( md5_context *ctx )
42{
43 ctx->total[0] = 0;
44 ctx->total[1] = 0;
45
46 ctx->state[0] = 0x67452301;
47 ctx->state[1] = 0xEFCDAB89;
48 ctx->state[2] = 0x98BADCFE;
49 ctx->state[3] = 0x10325476;
50}
51
52static void md5_process( md5_context *ctx, uint8_t data[64] )
53{
54 uint32_t X[16], A, B, C, D;
55
56 GET_UINT32( X[0], data, 0 );
57 GET_UINT32( X[1], data, 4 );
58 GET_UINT32( X[2], data, 8 );
59 GET_UINT32( X[3], data, 12 );
60 GET_UINT32( X[4], data, 16 );
61 GET_UINT32( X[5], data, 20 );
62 GET_UINT32( X[6], data, 24 );
63 GET_UINT32( X[7], data, 28 );
64 GET_UINT32( X[8], data, 32 );
65 GET_UINT32( X[9], data, 36 );
66 GET_UINT32( X[10], data, 40 );
67 GET_UINT32( X[11], data, 44 );
68 GET_UINT32( X[12], data, 48 );
69 GET_UINT32( X[13], data, 52 );
70 GET_UINT32( X[14], data, 56 );
71 GET_UINT32( X[15], data, 60 );
72
73#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
74
75#define P(a,b,c,d,k,s,t) \
76{ \
77 a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
78}
79
80 A = ctx->state[0];
81 B = ctx->state[1];
82 C = ctx->state[2];
83 D = ctx->state[3];
84
85#define F(x,y,z) (z ^ (x & (y ^ z)))
86
87 P( A, B, C, D, 0, 7, 0xD76AA478 );
88 P( D, A, B, C, 1, 12, 0xE8C7B756 );
89 P( C, D, A, B, 2, 17, 0x242070DB );
90 P( B, C, D, A, 3, 22, 0xC1BDCEEE );
91 P( A, B, C, D, 4, 7, 0xF57C0FAF );
92 P( D, A, B, C, 5, 12, 0x4787C62A );
93 P( C, D, A, B, 6, 17, 0xA8304613 );
94 P( B, C, D, A, 7, 22, 0xFD469501 );
95 P( A, B, C, D, 8, 7, 0x698098D8 );
96 P( D, A, B, C, 9, 12, 0x8B44F7AF );
97 P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
98 P( B, C, D, A, 11, 22, 0x895CD7BE );
99 P( A, B, C, D, 12, 7, 0x6B901122 );
100 P( D, A, B, C, 13, 12, 0xFD987193 );
101 P( C, D, A, B, 14, 17, 0xA679438E );
102 P( B, C, D, A, 15, 22, 0x49B40821 );
103
104#undef F
105
106#define F(x,y,z) (y ^ (z & (x ^ y)))
107
108 P( A, B, C, D, 1, 5, 0xF61E2562 );
109 P( D, A, B, C, 6, 9, 0xC040B340 );
110 P( C, D, A, B, 11, 14, 0x265E5A51 );
111 P( B, C, D, A, 0, 20, 0xE9B6C7AA );
112 P( A, B, C, D, 5, 5, 0xD62F105D );
113 P( D, A, B, C, 10, 9, 0x02441453 );
114 P( C, D, A, B, 15, 14, 0xD8A1E681 );
115 P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
116 P( A, B, C, D, 9, 5, 0x21E1CDE6 );
117 P( D, A, B, C, 14, 9, 0xC33707D6 );
118 P( C, D, A, B, 3, 14, 0xF4D50D87 );
119 P( B, C, D, A, 8, 20, 0x455A14ED );
120 P( A, B, C, D, 13, 5, 0xA9E3E905 );
121 P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
122 P( C, D, A, B, 7, 14, 0x676F02D9 );
123 P( B, C, D, A, 12, 20, 0x8D2A4C8A );
124
125#undef F
126
127#define F(x,y,z) (x ^ y ^ z)
128
129 P( A, B, C, D, 5, 4, 0xFFFA3942 );
130 P( D, A, B, C, 8, 11, 0x8771F681 );
131 P( C, D, A, B, 11, 16, 0x6D9D6122 );
132 P( B, C, D, A, 14, 23, 0xFDE5380C );
133 P( A, B, C, D, 1, 4, 0xA4BEEA44 );
134 P( D, A, B, C, 4, 11, 0x4BDECFA9 );
135 P( C, D, A, B, 7, 16, 0xF6BB4B60 );
136 P( B, C, D, A, 10, 23, 0xBEBFBC70 );
137 P( A, B, C, D, 13, 4, 0x289B7EC6 );
138 P( D, A, B, C, 0, 11, 0xEAA127FA );
139 P( C, D, A, B, 3, 16, 0xD4EF3085 );
140 P( B, C, D, A, 6, 23, 0x04881D05 );
141 P( A, B, C, D, 9, 4, 0xD9D4D039 );
142 P( D, A, B, C, 12, 11, 0xE6DB99E5 );
143 P( C, D, A, B, 15, 16, 0x1FA27CF8 );
144 P( B, C, D, A, 2, 23, 0xC4AC5665 );
145
146#undef F
147
148#define F(x,y,z) (y ^ (x | ~z))
149
150 P( A, B, C, D, 0, 6, 0xF4292244 );
151 P( D, A, B, C, 7, 10, 0x432AFF97 );
152 P( C, D, A, B, 14, 15, 0xAB9423A7 );
153 P( B, C, D, A, 5, 21, 0xFC93A039 );
154 P( A, B, C, D, 12, 6, 0x655B59C3 );
155 P( D, A, B, C, 3, 10, 0x8F0CCC92 );
156 P( C, D, A, B, 10, 15, 0xFFEFF47D );
157 P( B, C, D, A, 1, 21, 0x85845DD1 );
158 P( A, B, C, D, 8, 6, 0x6FA87E4F );
159 P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
160 P( C, D, A, B, 6, 15, 0xA3014314 );
161 P( B, C, D, A, 13, 21, 0x4E0811A1 );
162 P( A, B, C, D, 4, 6, 0xF7537E82 );
163 P( D, A, B, C, 11, 10, 0xBD3AF235 );
164 P( C, D, A, B, 2, 15, 0x2AD7D2BB );
165 P( B, C, D, A, 9, 21, 0xEB86D391 );
166
167#undef F
168
169 ctx->state[0] += A;
170 ctx->state[1] += B;
171 ctx->state[2] += C;
172 ctx->state[3] += D;
173}
174
175void md5_update( md5_context *ctx, uint8_t *input, uint32_t length )
176{
177 uint32_t left, fill;
178
179 if( ! length ) return;
180
181 left = ctx->total[0] & 0x3F;
182 fill = 64 - left;
183
184 ctx->total[0] += length;
185 ctx->total[0] &= 0xFFFFFFFF;
186
187 if( ctx->total[0] < length )
188 ctx->total[1]++;
189
190 if( left && length >= fill )
191 {
192 memcpy( (void *) (ctx->buffer + left),
193 (void *) input, fill );
194 md5_process( ctx, ctx->buffer );
195 length -= fill;
196 input += fill;
197 left = 0;
198 }
199
200 while( length >= 64 )
201 {
202 md5_process( ctx, input );
203 length -= 64;
204 input += 64;
205 }
206
207 if( length )
208 {
209 memcpy( (void *) (ctx->buffer + left),
210 (void *) input, length );
211 }
212}
213
214static uint8_t md5_padding[64] =
215{
216 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
220};
221
222void md5_finish( md5_context *ctx, uint8_t digest[16] )
223{
224 uint32_t last, padn;
225 uint32_t high, low;
226 uint8_t msglen[8];
227
228 high = ( ctx->total[0] >> 29 )
229 | ( ctx->total[1] << 3 );
230 low = ( ctx->total[0] << 3 );
231
232 PUT_UINT32( low, msglen, 0 );
233 PUT_UINT32( high, msglen, 4 );
234
235 last = ctx->total[0] & 0x3F;
236 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
237
238 md5_update( ctx, md5_padding, padn );
239 md5_update( ctx, msglen, 8 );
240
241 PUT_UINT32( ctx->state[0], digest, 0 );
242 PUT_UINT32( ctx->state[1], digest, 4 );
243 PUT_UINT32( ctx->state[2], digest, 8 );
244 PUT_UINT32( ctx->state[3], digest, 12 );
245}
diff --git a/lib/x1000-installer/test_lib/md5.h b/lib/x1000-installer/test_lib/md5.h
deleted file mode 100644
index 882636ed9a..0000000000
--- a/lib/x1000-installer/test_lib/md5.h
+++ /dev/null
@@ -1,18 +0,0 @@
1#ifndef _MD5_H
2#define _MD5_H
3
4#include <stdint.h>
5
6typedef struct
7{
8 uint32_t total[2];
9 uint32_t state[4];
10 uint8_t buffer[64];
11}
12md5_context;
13
14void md5_starts( md5_context *ctx );
15void md5_update( md5_context *ctx, uint8_t *input, uint32_t length );
16void md5_finish( md5_context *ctx, uint8_t digest[16] );
17
18#endif /* md5.h */
diff --git a/lib/x1000-installer/test_lib/nand-x1000.h b/lib/x1000-installer/test_lib/nand-x1000.h
deleted file mode 100644
index f34f2ce026..0000000000
--- a/lib/x1000-installer/test_lib/nand-x1000.h
+++ /dev/null
@@ -1,112 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22/* Stripped down fake version of X1000 NAND API for testing purposes,
23 * uses a normal file to store the data */
24
25#ifndef __NAND_X1000_H__
26#define __NAND_X1000_H__
27
28#include <stdint.h>
29#include <stddef.h>
30#include <stdbool.h>
31
32#define NAND_SUCCESS 0
33#define NAND_ERR_UNKNOWN_CHIP (-1)
34#define NAND_ERR_PROGRAM_FAIL (-2)
35#define NAND_ERR_ERASE_FAIL (-3)
36#define NAND_ERR_UNALIGNED (-4)
37#define NAND_ERR_OTHER (-5)
38#define NAND_ERR_INJECTED (-6)
39
40/* keep max page size in sync with the NAND chip table in the .c file */
41#define NAND_DRV_SCRATCHSIZE 32
42#define NAND_DRV_MAXPAGESIZE 2112
43
44typedef uint32_t nand_block_t;
45typedef uint32_t nand_page_t;
46
47enum nand_trace_type {
48 NTT_READ,
49 NTT_PROGRAM,
50 NTT_ERASE,
51};
52
53enum nand_trace_exception {
54 NTE_NONE,
55 NTE_DOUBLE_PROGRAMMED,
56 NTE_CLEARED,
57};
58
59struct nand_trace {
60 enum nand_trace_type type;
61 enum nand_trace_exception exception;
62 nand_page_t addr;
63};
64
65typedef struct nand_chip {
66 /* Base2 logarithm of the number of pages per block */
67 unsigned log2_ppb;
68
69 /* Size of a page's main / oob areas, in bytes. */
70 unsigned page_size;
71 unsigned oob_size;
72
73 /* Total number of blocks in the chip */
74 unsigned nr_blocks;
75} nand_chip;
76
77typedef struct nand_drv {
78 /* Backing file */
79 int fd;
80 int metafd;
81 int lock_count;
82
83 unsigned refcount;
84 uint8_t* scratch_buf;
85 uint8_t* page_buf;
86 const nand_chip* chip;
87 unsigned ppb;
88 unsigned fpage_size;
89} nand_drv;
90
91extern const char* nand_backing_file;
92extern const char* nand_meta_file;
93
94extern struct nand_trace* nand_trace;
95extern size_t nand_trace_capacity;
96extern size_t nand_trace_length;
97
98extern void nand_trace_reset(size_t size);
99extern void nand_inject_error(int rc);
100
101extern nand_drv* nand_init(void);
102
103extern void nand_lock(nand_drv* drv);
104extern void nand_unlock(nand_drv* drv);
105
106extern int nand_open(nand_drv* drv);
107extern void nand_close(nand_drv* drv);
108extern int nand_block_erase(nand_drv* drv, nand_block_t block);
109extern int nand_page_program(nand_drv* drv, nand_page_t page, const void* buffer);
110extern int nand_page_read(nand_drv* drv, nand_page_t page, void* buffer);
111
112#endif /* __NAND_X1000_H__ */
diff --git a/lib/x1000-installer/test_lib/pathfuncs.c b/lib/x1000-installer/test_lib/pathfuncs.c
deleted file mode 100644
index 341efd4730..0000000000
--- a/lib/x1000-installer/test_lib/pathfuncs.c
+++ /dev/null
@@ -1,130 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2014 by Michael Sevakis
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "pathfuncs.h"
23#include "strlcpy.h"
24#include "system.h"
25#include <string.h>
26
27static const char* GOBBLE_PATH_SEPCH(const char* p)
28{
29 int c;
30 while((c = *p) == PATH_SEPCH)
31 ++p;
32 return p;
33}
34
35static const char* GOBBLE_PATH_COMP(const char* p)
36{
37 int c;
38 while((c = *p) && c != PATH_SEPCH)
39 ++p;
40 return p;
41}
42
43/* Strips the trailing component from the path
44 * "" *nameptr->NUL, len=0: ""
45 * "/" *nameptr->/, len=1: "/"
46 * "//" *nameptr->2nd /, len=1: "/"
47 * "/a" *nameptr->/, len=1: "/"
48 * "a/" *nameptr->a, len=0: ""
49 * "/a/bc" *nameptr->/, len=2: "/a"
50 * "d" *nameptr->d, len=0: ""
51 * "ef/gh" *nameptr->e, len=2: "ef"
52 *
53 * Notes: * Interpret len=0 as ".".
54 * * In the same string, path_dirname() returns a pointer with the
55 * same or lower address as path_basename().
56 * * Pasting a separator between the returns of path_dirname() and
57 * path_basename() will result in a path equivalent to the input.
58 *
59 */
60size_t path_dirname(const char *name, const char **nameptr)
61{
62 const char *p = GOBBLE_PATH_SEPCH(name);
63 const char *q = name;
64 const char *r = p;
65
66 while (*(p = GOBBLE_PATH_COMP(p)))
67 {
68 const char *s = p;
69
70 if (!*(p = GOBBLE_PATH_SEPCH(p)))
71 break;
72
73 q = s;
74 }
75
76 if (q == name && r > name)
77 name = r, q = name--; /* root - return last slash */
78
79 *nameptr = name;
80 return q - name;
81}
82
83/* Appends one path to another, adding separators between components if needed.
84 * Return value and behavior is otherwise as strlcpy so that truncation may be
85 * detected.
86 *
87 * For basepath and component:
88 * PA_SEP_HARD adds a separator even if the base path is empty
89 * PA_SEP_SOFT adds a separator only if the base path is not empty
90 */
91size_t path_append(char *buf, const char *basepath,
92 const char *component, size_t bufsize)
93{
94 const char *base = basepath && basepath[0] ? basepath : buf;
95 if (!base)
96 return bufsize; /* won't work to get lengths from buf */
97
98 if (!buf)
99 bufsize = 0;
100
101 if (path_is_absolute(component))
102 {
103 /* 'component' is absolute; replace all */
104 basepath = component;
105 component = "";
106 }
107
108 /* if basepath is not null or empty, buffer contents are replaced,
109 otherwise buf contains the base path */
110 size_t len = base == buf ? strlen(buf) : my_strlcpy(buf, basepath, bufsize);
111
112 bool separate = false;
113
114 if (!basepath || !component)
115 separate = !len || base[len-1] != PATH_SEPCH;
116 else if (component[0])
117 separate = len && base[len-1] != PATH_SEPCH;
118
119 /* caller might lie about size of buf yet use buf as the base */
120 if (base == buf && bufsize && len >= bufsize)
121 buf[bufsize - 1] = '\0';
122
123 buf += len;
124 bufsize -= MIN(len, bufsize);
125
126 if (separate && (len++, bufsize > 0) && --bufsize > 0)
127 *buf++ = PATH_SEPCH;
128
129 return len + my_strlcpy(buf, component ?: "", bufsize);
130}
diff --git a/lib/x1000-installer/test_lib/pathfuncs.h b/lib/x1000-installer/test_lib/pathfuncs.h
deleted file mode 100644
index 225b3cdd19..0000000000
--- a/lib/x1000-installer/test_lib/pathfuncs.h
+++ /dev/null
@@ -1,39 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2014 by Michael Sevakis
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef _PATHFUNCS_H_
22#define _PATHFUNCS_H_
23
24#include <stddef.h>
25#include <stdbool.h>
26
27#define PATH_SEPCH '/'
28
29/* return true if path begins with a root '/' component and is not NULL */
30static inline bool path_is_absolute(const char *path)
31{
32 return path && path[0] == PATH_SEPCH;
33}
34
35size_t path_dirname(const char *name, const char **nameptr);
36size_t path_append(char *buf, const char *basepath,
37 const char *component, size_t bufsize);
38
39#endif
diff --git a/lib/x1000-installer/test_lib/strlcpy.c b/lib/x1000-installer/test_lib/strlcpy.c
deleted file mode 100644
index 681d917503..0000000000
--- a/lib/x1000-installer/test_lib/strlcpy.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <string.h>
20
21/*
22 * Copy src to string dst of size siz. At most siz-1 characters
23 * will be copied. Always NUL terminates (unless siz == 0).
24 * Returns strlen(src); if retval >= siz, truncation occurred.
25 */
26size_t
27my_strlcpy(char *dst, const char *src, size_t siz)
28{
29 char *d = dst;
30 const char *s = src;
31 size_t n = siz;
32
33 /* Copy as many bytes as will fit */
34 if (n != 0) {
35 while (--n != 0) {
36 if ((*d++ = *s++) == '\0')
37 break;
38 }
39 }
40
41 /* Not enough room in dst, add NUL and traverse rest of src */
42 if (n == 0) {
43 if (siz != 0)
44 *d = '\0'; /* NUL-terminate dst */
45 while (*s++)
46 ;
47 }
48
49 return(s - src - 1); /* count does not include NUL */
50}
diff --git a/lib/x1000-installer/test_lib/strlcpy.h b/lib/x1000-installer/test_lib/strlcpy.h
deleted file mode 100644
index 0c4b16dd5a..0000000000
--- a/lib/x1000-installer/test_lib/strlcpy.h
+++ /dev/null
@@ -1,4 +0,0 @@
1#ifndef STRLCPY_H
2#define STRLCPY_H
3size_t my_strlcpy(char *dst, const char *src, size_t siz);
4#endif
diff --git a/lib/x1000-installer/test_lib/system.h b/lib/x1000-installer/test_lib/system.h
deleted file mode 100644
index b0a5076ba9..0000000000
--- a/lib/x1000-installer/test_lib/system.h
+++ /dev/null
@@ -1,10 +0,0 @@
1#ifndef SYSTEM_H
2#define SYSTEM_H
3
4#define CACHEALIGN_SIZE 1
5#define CACHEALIGN_BUFFER(x,y) do { } while(0)
6#define MIN(a, b) (((a)<(b))?(a):(b))
7#define ALIGN_BUFFER(ptr, size, align) do { } while(0)
8#define ALIGN_UP_P2(x, p) (((x) + ((1 << (p)) - 1)) & ~((1 << (p)) - 1))
9
10#endif