2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 #include <openssl/md5.h>
32 ao_mutex_get(uint8_t *mutex)
37 ao_mutex_put(uint8_t *mutex)
42 ao_panic(uint8_t panic)
44 printf ("panic %d\n", panic);
48 #define AO_PANIC_BUFIO 15
50 #define ao_cmd_success 0
52 uint8_t ao_cmd_status;
53 uint32_t ao_cmd_lex_u32;
60 #define ao_cmd_register(x)
69 uint64_t total_reads, total_writes;
72 ao_sdcard_read_block(uint32_t block, uint8_t *data)
75 lseek(fs_fd, block * 512, 0);
76 return read(fs_fd, data, 512) == 512;
80 ao_sdcard_write_block(uint32_t block, uint8_t *data)
83 lseek(fs_fd, block * 512, 0);
84 return write(fs_fd, data, 512) == 512;
91 { .fat = 16, .blocks = 16384 },
92 { .fat = 32, .blocks = 16384 },
93 { .fat = 16, .blocks = 65536 },
94 { .fat = 32, .blocks = 65536 },
95 { .fat = 16, .blocks = 1048576 },
96 { .fat = 32, .blocks = 1048576 },
97 { .fat = 0, .blocks = 0 },
101 struct fs_param *param;
112 snprintf(cmd, sizeof(cmd), "rm -f %s && mkfs.vfat -F %d -C %s %d",
113 fs, param->fat, fs, param->blocks);
114 if (system (cmd) != 0) {
115 fprintf(stderr, "'%s' failed\n", cmd);
125 #include "ao_bufio.c"
127 check_bufio(char *where)
131 for (b = 0; b < AO_NUM_BUF; b++) {
132 if (ao_bufio[b].busy) {
133 printf ("%s: buffer %d busy. block %d seqno %u\n",
134 where, b, ao_bufio[b].block, ao_bufio[b].seqno & 0xffff);
146 /* Get the next cluster entry in the chain */
148 ao_fat_entry_raw_read(cluster_t cluster, uint8_t fat)
151 cluster_offset_t offset;
159 sector = cluster >> SECTOR_SHIFT;
160 offset = cluster & SECTOR_MASK;
161 buf = _ao_fat_sector_get(fat_start + fat * sectors_per_fat + sector);
165 ret = get_u32(buf + offset);
167 ret = get_u16(buf + offset);
168 _ao_fat_sector_put(buf, 0);
177 printf ("\n **** FAT ****\n\n");
178 for (e = 0; e < number_cluster; e++) {
179 if ((e & 0xf) == 0x0)
180 printf ("%04x: ", e);
182 printf (" %08x", ao_fat_entry_raw_read(e, 0));
184 printf (" %04x", ao_fat_entry_raw_read(e, 0));
185 if ((e & 0xf) == 0xf)
196 struct ao_fat_dirent dirent;
198 printf (" **** Root directory ****\n");
199 while (ao_fat_readdir(&entry, &dirent)) {
200 printf ("%04x: %-8.8s.%-3.3s %02x %04x %d\n",
209 printf (" **** End of root directory ****\n");
213 fatal(char *msg, ...)
220 vfprintf(stderr, msg, l);
232 for (e = 0; e < number_cluster; e++) {
233 cluster_t v = ao_fat_entry_raw_read(e, 0);
234 for (f = 1; f < number_fat; f++) {
235 cluster_t o = ao_fat_entry_raw_read(e, f);
237 fatal ("fats differ at %08x (0 %08x %d %08x)\n", e, v, f, o);
243 check_file(dirent_t dent, cluster_t first_cluster, dirent_t *used)
245 cluster_t clusters = 0;
251 for (cluster = first_cluster;
252 fat32 ? !AO_FAT_IS_LAST_CLUSTER(cluster) : !AO_FAT_IS_LAST_CLUSTER16(cluster);
253 cluster = ao_fat_entry_raw_read(cluster, 0))
255 if (!_ao_fat_cluster_valid(cluster))
256 fatal("file %d: invalid cluster %08x\n", dent, cluster);
258 fatal("file %d: duplicate cluster %08x also in file %d\n", dent, cluster, used[cluster]-1);
259 used[cluster] = dent;
269 cluster_t cluster, chain;
275 used = calloc(sizeof (dirent_t), number_cluster);
277 for (r = 0; (dent = _ao_fat_root_get(r)); r++) {
280 cluster_t first_cluster;
284 fatal("cannot map dent %d\n", r);
285 memcpy(name, dent+0, 11);
286 first_cluster = get_u16(dent + 0x1a);
288 first_cluster |= (cluster_t) get_u16(dent + 0x14) << 16;
289 size = get_u32(dent + 0x1c);
290 _ao_fat_root_put(dent, r, 0);
292 if (name[0] == AO_FAT_DENT_END) {
296 clusters = check_file(r + 1, first_cluster, used);
299 fatal("file %d: zero sized, but %d clusters\n", clusters);
301 if (size > clusters * bytes_per_cluster)
302 fatal("file %d: size %u beyond clusters %d (%u)\n",
303 r, size, clusters, clusters * bytes_per_cluster);
304 if (size <= (clusters - 1) * bytes_per_cluster)
305 fatal("file %d: size %u too small clusters %d (%u)\n",
306 r, size, clusters, clusters * bytes_per_cluster);
310 for (; r < root_entries; r++) {
311 uint8_t *dent = _ao_fat_root_get(r);
313 fatal("cannot map dent %d\n", r);
314 if (dent[0] != AO_FAT_DENT_END)
315 fatal("found non-zero dent past end %d\n", r);
316 _ao_fat_root_put(dent, r, 0);
319 check_file((dirent_t) -1, root_cluster, used);
322 for (cluster = 0; cluster < 2; cluster++) {
323 chain = ao_fat_entry_raw_read(cluster, 0);
326 if ((chain & 0xffffff8) != 0xffffff8)
327 fatal("cluster %d: not marked busy\n", cluster);
329 if ((chain & 0xfff8) != 0xfff8)
330 fatal("cluster %d: not marked busy\n", cluster);
333 for (; cluster < number_cluster; cluster++) {
334 chain = ao_fat_entry_raw_read(cluster, 0);
337 if (used[cluster] == 0)
338 fatal("cluster %d: marked busy, but not in any file\n", cluster);
340 if (used[cluster] != 0)
341 fatal("cluster %d: marked free, but found in file %d\n", cluster, used[cluster]-1);
346 #define NUM_FILES 100
347 #define LINES_FILE 500000
349 uint32_t sizes[NUM_FILES];
351 unsigned char md5[NUM_FILES][MD5_DIGEST_LENGTH];
357 char name[] = "FOO ";
361 printf ("write once\n");
362 if ((fd = ao_fat_creat(name)) >= 0) {
363 ao_fat_write(fd, "hello world\n", 12);
367 printf ("read first\n");
368 if ((fd = ao_fat_open(name, AO_FAT_OPEN_READ)) >= 0) {
369 len = ao_fat_read(fd, buf, sizeof(buf));
374 printf ("write again\n");
375 if ((fd = ao_fat_creat(name)) >= 0) {
376 ao_fat_write(fd, "hi\n", 3);
380 printf ("read again\n");
381 if ((fd = ao_fat_open(name, AO_FAT_OPEN_READ)) >= 0) {
382 len = ao_fat_read(fd, buf, sizeof(buf));
387 printf ("write 3\n");
388 if ((fd = ao_fat_creat(name)) >= 0) {
392 for (l = 0; l < 10; l++) {
393 for (c = ' '; c < '~'; c++)
394 ao_fat_write(fd, &c, 1);
396 ao_fat_write(fd, &c, 1);
402 if ((fd = ao_fat_open(name, AO_FAT_OPEN_READ)) >= 0) {
403 while ((len = ao_fat_read(fd, buf, sizeof(buf))) > 0)
409 printf ("all done\n");
420 if ((fd = ao_fat_open("HELLO TXT",AO_FAT_OPEN_READ)) >= 0) {
421 printf ("File contents for HELLO.TXT\n");
422 while ((len = ao_fat_read(fd, buf, sizeof(buf))))
427 if ((fd = ao_fat_creat("NEWFILE TXT")) >= 0) {
428 printf ("Create new file\n");
429 for (len = 0; len < 2; len++)
430 ao_fat_write(fd, "hello, world!\n", 14);
431 ao_fat_seek(fd, 0, AO_FAT_SEEK_SET);
432 printf ("read new file\n");
433 while ((len = ao_fat_read(fd, buf, sizeof (buf))))
447 unsigned char md5_check[MD5_DIGEST_LENGTH];
451 uint64_t total_file_size = 0;
453 total_reads = total_writes = 0;
455 printf (" **** Creating %d files\n", NUM_FILES);
457 memset(sizes, '\0', sizeof (sizes));
458 for (id = 0; id < NUM_FILES; id++) {
459 sprintf(name, "D%07dTXT", id);
460 if ((id % ((NUM_FILES+49)/50)) == 0) {
461 printf ("."); fflush(stdout);
463 if ((fd = ao_fat_creat(name)) >= 0) {
466 check_bufio("file created");
468 for (j = 0; j < LINES_FILE; j++) {
470 sprintf (line, "Hello, world %d %d\r\n", id, j);
472 ret = ao_fat_write(fd, line, len);
475 total_file_size += ret;
476 MD5_Update(&ctx, line, ret);
479 printf ("write failed %d\n", ret);
482 MD5_Final(&md5[id][0], &ctx);
483 check_bufio("file written");
487 printf ("\n **** Write IO: read %llu write %llu data sectors %llu\n", total_reads, total_writes, (total_file_size + 511) / 512);
489 check_bufio("all files created");
490 printf (" **** All done creating files\n");
493 total_reads = total_writes = 0;
495 printf (" **** Comparing %d files\n", NUM_FILES);
497 for (id = 0; id < NUM_FILES; id++) {
499 sprintf(name, "D%07dTXT", id);
501 if ((id % ((NUM_FILES+49)/50)) == 0) {
502 printf ("."); fflush(stdout);
504 if ((fd = ao_fat_open(name, AO_FAT_OPEN_READ)) >= 0) {
506 while ((len = ao_fat_read(fd, buf, sizeof(buf))) > 0) {
507 MD5_Update(&ctx, buf, len);
511 MD5_Final(md5_check, &ctx);
512 if (size != sizes[id])
513 fatal("file %d: size differs %d written %d read\n",
514 id, sizes[id], size);
515 if (memcmp(md5_check, &md5[id][0], sizeof (md5_check)) != 0)
516 fatal ("file %d: checksum failed\n", id);
517 check_bufio("file shown");
520 printf ("\n **** Read IO: read %llu write %llu\n", total_reads, total_writes);
528 "-F 16 -C %s 1048576",
529 "-F 32 -C %s 1048576",
534 do_test(void (*test)(void))
542 check_bufio("after setup");
548 main(int argc, char **argv)
555 for (p = 0; fs_params[p].fat; p++) {
556 param = &fs_params[p];
558 do_test(micro_test_fs);
559 do_test(short_test_fs);
560 do_test(long_test_fs);