LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
main.c
Go to the documentation of this file.
1 /*
2  Test program for DOSFS
3  Lewin A.R.W. Edwards (sysadm@zws.com)
4 */
5 
6 #include <stdio.h>
7 #include <sys/types.h>
8 
9 #include "dosfs.h"
10 
11 
12 
13 int main(int _argc, char *_argv[])
14 {
15  uint8_t sector[SECTOR_SIZE], sector2[SECTOR_SIZE];
16  uint32_t pstart, psize, i;
17  uint8_t pactive, ptype;
18  VOLINFO vi;
19  DIRINFO di;
20  DIRENT de;
21  uint32_t cache;
22  FILEINFO fi;
23  uint8_t *p;
24 
25  if (_argc < 2) {
26  printf("Usage: %s [image_file]\n", _argv[0]);
27  return -1;
28  }
29 
30  // Attach user-specified image file
31  if (DFS_HostAttach(_argv[1])) {
32  printf("Cannot attach image file '%s'\n", _argv[1]);
33  return -1;
34  }
35 
36  // Obtain pointer to first partition on first (only) unit
37  pstart = DFS_GetPtnStart(0, sector, 0, &pactive, &ptype, &psize);
38  if (pstart == 0xffffffff) {
39  printf("Cannot find first partition\n");
40  return -1;
41  }
42 
43  printf("Partition 0 start sector 0x%-08.8lX active %-02.2hX type %-02.2hX size %-08.8lX\n", pstart, pactive, ptype, psize);
44 
45  if (DFS_GetVolInfo(0, sector, pstart, &vi)) {
46  printf("Error getting volume information\n");
47  return -1;
48  }
49  printf("Volume label '%-11.11s'\n", vi.label);
50  printf("%d sector/s per cluster, %d reserved sector/s, volume total %d sectors.\n", vi.secperclus, vi.reservedsecs, vi.numsecs);
51  printf("%d sectors per FAT, first FAT at sector #%d, root dir at #%d.\n",vi.secperfat,vi.fat1,vi.rootdir);
52  printf("(For FAT32, the root dir is a CLUSTER number, FAT12/16 it is a SECTOR number)\n");
53  printf("%d root dir entries, data area commences at sector #%d.\n",vi.rootentries,vi.dataarea);
54  printf("%d clusters (%d bytes) in data area, filesystem IDd as ", vi.numclusters, vi.numclusters * vi.secperclus * SECTOR_SIZE);
55  if (vi.filesystem == FAT12)
56  printf("FAT12.\n");
57  else if (vi.filesystem == FAT16)
58  printf("FAT16.\n");
59  else if (vi.filesystem == FAT32)
60  printf("FAT32.\n");
61  else
62  printf("[unknown]\n");
63 
64 //------------------------------------------------------------
65 // Directory enumeration test
66  di.scratch = sector;
67 // if (DFS_OpenDir(&vi, "", &di)) {
68 // printf("Error opening root directory\n");
69 // return -1;
70 // }
71  if (DFS_OpenDir(&vi, "MYDIR1", &di)) {
72  printf("error opening subdirectory\n");
73  return -1;
74  }
75  while (!DFS_GetNext(&vi, &di, &de)) {
76  if (de.name[0])
77  printf("file: '%-11.11s'\n", de.name);
78  }
79 
80 
81 //------------------------------------------------------------
82 // Unlink test
83 // cache = 0;
84 // printf("*** FAT BEFORE ***\n");
85 // for (i=0;i<vi.numclusters;i++) {
86 // printf("entry %-08.8x, %-08.8X\n", i, DFS_GetFAT(&vi, sector, &cache, i));
87 // }
88 // if (DFS_UnlinkFile(&vi, "MYDIR1/SUBDIR1.2/README.TXT", sector)) {
89 // printf("error unlinking file\n");
90 // }
91 // printf("*** FAT AFTER ***\n");
92 // for (i=0;i<vi.numclusters;i++) {
93 // printf("entry %-08.8x, %-08.8X\n", i, DFS_GetFAT(&vi, sector, &cache, i));
94 // }
95 
96 //------------------------------------------------------------
97 // File write test
98  if (DFS_OpenFile(&vi, "MYDIR1/WRTEST.TXT", DFS_WRITE, sector, &fi)) {
99  printf("error opening file\n");
100  return -1;
101  }
102  for (i=0;i<18;i++) {
103  memset(sector2, 128+i, SECTOR_SIZE);
104  DFS_WriteFile(&fi, sector, sector2, &cache, SECTOR_SIZE/2);
105  memset(sector2+256, 255-i, SECTOR_SIZE/2);
106  DFS_WriteFile(&fi, sector, sector2+256, &cache, SECTOR_SIZE/2);
107  }
108  sprintf(sector2, "test string at the end...");
109  DFS_WriteFile(&fi, sector, sector2, &cache, strlen(sector2));
110 
111 //------------------------------------------------------------
112 // File read test
113  printf("Readback test\n");
114  if (DFS_OpenFile(&vi, "MYDIR1/WRTEST.TXT", DFS_READ, sector, &fi)) {
115  printf("error opening file\n");
116  return -1;
117  }
118  p = (void *) malloc(fi.filelen+512);
119  memset(p, 0xaa, fi.filelen+512);
120 
121  DFS_ReadFile(&fi, sector, p, &i, fi.filelen);
122 printf("read complete %d bytes (expected %d) pointer %d\n", i, fi.filelen, fi.pointer);
123 
124  {
125  FILE *fp;
126  fp=fopen("test.txt","wb");
127  fwrite(p, fi.filelen+512, 1, fp);
128  fclose(fp);
129  }
130 
131  return 0;
132 }