aboutsummaryrefslogtreecommitdiff
path: root/mkfs.ffs2.c
blob: 36204ddb4c07e1017f647e3feb3fe3de1ae82e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Description
// $Id: mkfs.ffs2.c,v 1.5 2005/11/07 11:15:12 gleixner Exp $
/* ######################################################################

   Microsoft Flash File System 2

   Information for the FFS2.0 was found in Microsoft's knowledge base,
   http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/specs/S346A.HTM
   Try searching for "Flash File System" if it has been moved

   This program creates an empty file system.

   ##################################################################### */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/mount.h>

#include <mtd/mtd-user.h"
#include <linux/ffs2_fs.h"

static unsigned long BlockSize = 128*1024;
static int Fd;

// Erase a single block
int EraseBlock(unsigned Number)
{
   unsigned char Blank[512];
   unsigned I;

   memset(Blank,0xFF,sizeof(Blank));

   if (lseek(Fd,BlockSize*Number,SEEK_SET) != BlockSize*Number)
      return -1;

   for (I = 0; I*sizeof(Blank) != BlockSize; I++)
   {
      if (write(Fd,Blank,sizeof(Blank)) != sizeof(Blank))
      return -1;
   }

   return 0;
}

int main (int argc,char * const argv[])
{
   struct mtd_info meminfo;
   unsigned Device;
   unsigned int Opt;
   unsigned I;
   unsigned long Length;
   unsigned long Spares = 1;
   unsigned long Start = 0;

   // Process options
   while ((Opt = getopt(argc,argv,"b:s:")) != EOF)
   {
      switch (Opt)
      {
	 case 'b':
	 BlockSize = strtol(optarg, NULL, 0);
	 break;

	 case 's':
	 Start = strtol(optarg, NULL, 0);
	 break;

	 case '?':
	 return 16;
      }
   }

   // Find the device name
   Device = optind;
   for (;Device < argc && (argv[Device][0] == 0 ||
			   argv[Device][0] == '-'); Device++);
   if (Device >= argc)
   {
      fprintf(stderr,"You must specify a device\n");
      return 16;
   }

   // Open and size the device
   if ((Fd = open(argv[Device],O_RDWR)) < 0)
   {
      fprintf(stderr,"File open error\n");
      return 8;
   }
   if (ioctl(Fd,BLKGETSIZE,&Length) < 0)
   {
      Length = lseek(Fd,0,SEEK_END);
      lseek(Fd,0,SEEK_SET);
   }
   else
      Length *= 512;

   if ((Start + 1)*BlockSize > Length)
   {
      fprintf(stderr,"The flash is not large enough\n");
   }

   printf("Total size is %lu, %lu byte erase "
	  "blocks for %lu blocks with %lu spares.\n",Length,BlockSize,
	  Length/BlockSize,Spares);
   if (Start != 0)
      printf("Skiping the first %lu bytes\n",Start*BlockSize);

   if (ioctl(Fd,MEMGETINFO,&meminfo) == 0)
   {
      struct erase_info erase;
      printf("Performing Flash Erase");
      fflush(stdout);

      erase.length = Length - Start*BlockSize;
      erase.start = Start*BlockSize;
      if (ioctl(Fd,MEMERASE,&erase) != 0)
      {
	 perror("\nMTD Erase failure");
	 close(Fd);
	 return 8;
      }
      printf(" done\n");
   }
   else
   {
      for (I = Start; I <= Length/BlockSize; I++)
      {
	 printf("Erase %u\r",I);
	 fflush(stdout);
	 if (EraseBlock(I) != 0)
	 {
	    perror(argv[Device]);
	    close(Fd);
	    return 8;
	 }
      }
   }

   for (I = 0; I != Length/BlockSize; I++)
   {
      struct ffs2_block block;

      // Write the block structure
      memset(&block,0xFF,sizeof(block));
      block.EraseCount = 1;
      block.BlockSeq = I;
      block.BlockSeqChecksum = 0xFFFF ^ block.BlockSeq;
      block.Status = (block.Status & (~FFS_STATE_MASK)) | FFS_STATE_READY;

      // Is Spare
      if (I >= Length/BlockSize - Spares)
      {
	 block.BlockSeq = 0xFFFF;
	 block.BlockSeqChecksum = 0xFFFF;
	 block.Status = (block.Status & (~FFS_STATE_MASK)) | FFS_STATE_SPARE;
      }

      // Setup the boot record and the root record
      if (I == 0)
      {
	 struct ffs2_bootrecord boot;
	 struct ffs2_blockalloc alloc[2];
	 unsigned char Tmp[300];
	 struct ffs2_entry *root = (struct ffs2_entry *)Tmp;

	 block.BootRecordPtr = 0;
	 block.Status = (block.Status & (~FFS_BOOTP_MASK)) | FFS_BOOTP_CURRENT;

	 boot.Signature = 0xF1A5;
	 boot.SerialNumber = time(0);
	 boot.FFSWriteVersion = 0x200;
	 boot.FFSReadVersion = 0x200;
	 boot.TotalBlockCount = Length/BlockSize;
	 boot.SpareBlockCount = Spares;
	 boot.BlockLen = BlockSize;
	 boot.RootDirectoryPtr = 0x1;
	 boot.BootCodeLen = 0;

	 memset(root,0xFF,sizeof(*root));
	 root->Status = (root->Status & (~FFS_ENTRY_TYPEMASK)) | FFS_ENTRY_TYPEDIR;
	 root->NameLen = strlen("root");
	 root->Time = (__u16)boot.SerialNumber;
	 root->Date = (__u16)(boot.SerialNumber >> 16);
	 root->VarStructLen = 0;
	 strcpy(root->Name,"root");

	 // Boot Block allocation structure
	 alloc[1].Status = (0xFF & (~FFS_ALLOC_SMASK)) | FFS_ALLOC_ALLOCATED;
	 alloc[1].Status &= 0xFF & (~FFS_ALLOC_EMASK);
	 alloc[1].Offset[0] = 0;
	 alloc[1].Offset[1] = 0;
	 alloc[1].Offset[2] = 0;
	 alloc[1].Len = FFS_SIZEOF_BOOT;

	 // Root Dir allocation structure
	 alloc[0].Status = (0xFF & (~FFS_ALLOC_SMASK)) | FFS_ALLOC_ALLOCATED;
	 alloc[0].Offset[0] = FFS_SIZEOF_BOOT;
	 alloc[0].Offset[1] = 0;
	 alloc[0].Offset[2] = 0;
	 alloc[0].Len = FFS_SIZEOF_ENTRY + root->NameLen;

	 // Write the two headers
	 if (lseek(Fd,BlockSize*(I+Start),SEEK_SET) < 0 ||
	     write(Fd,&boot,FFS_SIZEOF_BOOT) != FFS_SIZEOF_BOOT ||
	     write(Fd,root,FFS_SIZEOF_ENTRY + root->NameLen) != FFS_SIZEOF_ENTRY + root->NameLen)
	 {
	    perror("Failed writing headers");
	    close(Fd);
	    return 8;
	 }

	 // And the two allocation structures
	 if (lseek(Fd,BlockSize*(I+Start+1) - FFS_SIZEOF_BLOCK - sizeof(alloc),
		   SEEK_SET) <= 0 ||
	     write(Fd,alloc,sizeof(alloc)) != sizeof(alloc))
	 {
	    perror("Failed writing allocations");
	    close(Fd);
	    return 8;
	 }
      }

      if (lseek(Fd,BlockSize*(I+Start+1) - FFS_SIZEOF_BLOCK,
		SEEK_SET) <= 0 ||
	  write(Fd,&block,FFS_SIZEOF_BLOCK) != FFS_SIZEOF_BLOCK)
      {
	 perror("Failed writing block");
	 close(Fd);
	 return 8;
      }
   }
   printf("\n");

   return 0;
}