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 if (ao_fat_entry_raw_read(e, f) != v)
236 fatal ("fats differ at %d\n", e);
242 check_file(dirent_t dent, cluster_t first_cluster, dirent_t *used)
244 cluster_t clusters = 0;
250 for (cluster = first_cluster;
251 fat32 ? !AO_FAT_IS_LAST_CLUSTER(cluster) : !AO_FAT_IS_LAST_CLUSTER16(cluster);
252 cluster = ao_fat_entry_raw_read(cluster, 0))
254 if (!ao_fat_cluster_valid(cluster))
255 fatal("file %d: invalid cluster %08x\n", dent, cluster);
257 fatal("file %d: duplicate cluster %08x also in file %d\n", dent, cluster, used[cluster]-1);
258 used[cluster] = dent;
268 cluster_t cluster, chain;
274 used = calloc(sizeof (dirent_t), number_cluster);
276 for (r = 0; (dent = ao_fat_root_get(r)); r++) {
279 cluster_t first_cluster;
283 fatal("cannot map dent %d\n", r);
284 memcpy(name, dent+0, 11);
285 first_cluster = get_u16(dent + 0x1a);
287 first_cluster |= (cluster_t) get_u16(dent + 0x14) << 16;
288 size = get_u32(dent + 0x1c);
289 ao_fat_root_put(dent, r, 0);
291 if (name[0] == AO_FAT_DENT_END) {
295 clusters = check_file(r + 1, first_cluster, used);
298 fatal("file %d: zero sized, but %d clusters\n", clusters);
300 if (size > clusters * bytes_per_cluster)
301 fatal("file %d: size %u beyond clusters %d (%u)\n",
302 r, size, clusters, clusters * bytes_per_cluster);
303 if (size <= (clusters - 1) * bytes_per_cluster)
304 fatal("file %d: size %u too small clusters %d (%u)\n",
305 r, size, clusters, clusters * bytes_per_cluster);
309 for (; r < root_entries; r++) {
310 uint8_t *dent = ao_fat_root_get(r);
312 fatal("cannot map dent %d\n", r);
313 if (dent[0] != AO_FAT_DENT_END)
314 fatal("found non-zero dent past end %d\n", r);
315 ao_fat_root_put(dent, r, 0);
318 check_file((dirent_t) -1, root_cluster, used);
321 for (cluster = 0; cluster < 2; cluster++) {
322 chain = ao_fat_entry_raw_read(cluster, 0);
325 if ((chain & 0xffffff8) != 0xffffff8)
326 fatal("cluster %d: not marked busy\n", cluster);
328 if ((chain & 0xfff8) != 0xfff8)
329 fatal("cluster %d: not marked busy\n", cluster);
332 for (; cluster < number_cluster; cluster++) {
333 chain = ao_fat_entry_raw_read(cluster, 0);
336 if (used[cluster] == 0)
337 fatal("cluster %d: marked busy, but not in any file\n", cluster);
339 if (used[cluster] != 0)
340 fatal("cluster %d: marked free, but found in file %d\n", cluster, used[cluster]-1);
345 #define NUM_FILES 100
346 #define LINES_FILE 500000
348 uint32_t sizes[NUM_FILES];
350 unsigned char md5[NUM_FILES][MD5_DIGEST_LENGTH];
358 if (ao_fat_open("HELLO TXT",AO_FAT_OPEN_READ) == AO_FAT_SUCCESS) {
359 printf ("File contents for HELLO.TXT\n");
360 while ((len = ao_fat_read(buf, sizeof(buf))))
365 if (ao_fat_creat("NEWFILE TXT") == AO_FAT_SUCCESS) {
366 printf ("Create new file\n");
367 for (len = 0; len < 2; len++)
368 ao_fat_write("hello, world!\n", 14);
369 ao_fat_seek(0, AO_FAT_SEEK_SET);
370 printf ("read new file\n");
371 while ((len = ao_fat_read(buf, sizeof (buf))))
385 unsigned char md5_check[MD5_DIGEST_LENGTH];
388 uint64_t total_file_size = 0;
390 total_reads = total_writes = 0;
392 printf (" **** Creating %d files\n", NUM_FILES);
394 memset(sizes, '\0', sizeof (sizes));
395 for (id = 0; id < NUM_FILES; id++) {
396 sprintf(name, "D%07dTXT", id);
397 if ((id % (NUM_FILES/50)) == 0) {
398 printf ("."); fflush(stdout);
400 if (ao_fat_creat(name) == AO_FAT_SUCCESS) {
403 check_bufio("file created");
405 for (j = 0; j < LINES_FILE; j++) {
407 sprintf (line, "Hello, world %d %d\r\n", id, j);
409 ret = ao_fat_write((uint8_t *) line, len);
412 total_file_size += ret;
413 MD5_Update(&ctx, line, ret);
416 printf ("write failed %d\n", ret);
419 MD5_Final(&md5[id][0], &ctx);
420 check_bufio("file written");
424 printf ("\n **** Write IO: read %llu write %llu data sectors %llu\n", total_reads, total_writes, (total_file_size + 511) / 512);
426 check_bufio("all files created");
427 printf (" **** All done creating files\n");
430 total_reads = total_writes = 0;
432 printf (" **** Comparing %d files\n", NUM_FILES);
434 for (id = 0; id < NUM_FILES; id++) {
436 sprintf(name, "D%07dTXT", id);
438 if ((id % (NUM_FILES/50)) == 0) {
439 printf ("."); fflush(stdout);
441 if (ao_fat_open(name, AO_FAT_OPEN_READ) == AO_FAT_SUCCESS) {
443 while ((len = ao_fat_read((uint8_t *) buf, sizeof(buf))) > 0) {
444 MD5_Update(&ctx, buf, len);
448 MD5_Final(md5_check, &ctx);
449 if (size != sizes[id])
450 fatal("file %d: size differs %d written %d read\n",
451 id, sizes[id], size);
452 if (memcmp(md5_check, &md5[id][0], sizeof (md5_check)) != 0)
453 fatal ("file %d: checksum failed\n", id);
454 check_bufio("file shown");
457 printf ("\n **** Read IO: read %llu write %llu\n", total_reads, total_writes);
465 "-F 16 -C %s 1048576",
466 "-F 32 -C %s 1048576",
471 main(int argc, char **argv)
478 for (p = 0; fs_params[p].fat; p++) {
479 param = &fs_params[p];
486 check_bufio("after setup");