Previous 15994 Revisions Next

r5845 Tuesday 22nd September, 2009 at 12:42:33 UTC by Dirk Best
[IMGTOOL] Added (very preliminary and not fully working) support for Amiga OFS/FFS filesystems
[src/mess/formats]ami_dsk.c* ami_dsk.h*
[src/mess/tools/imgtool]imgtool.mak modules.c
[src/mess/tools/imgtool/modules]amiga.c*

src/mess/tools/imgtool/imgtool.mak
r5844r5845
6464   $(IMGTOOLOBJ)/imgterrs.o         \
6565   $(IMGTOOLOBJ)/imghd.o            \
6666   $(IMGTOOLOBJ)/charconv.o         \
67   $(IMGTOOL_MODULES)/amiga.o         \
6768   $(IMGTOOL_MODULES)/macbin.o         \
6869   $(IMGTOOL_MODULES)/rsdos.o         \
6970   $(IMGTOOL_MODULES)/os9.o         \
src/mess/tools/imgtool/modules.c
r5844r5845
8787
8888#else /* MODULES_RECURSIVE */
8989
90MODULE(amiga_floppy)
9091MODULE(concept)
9192MODULE(mac_mfs)
9293MODULE(mac_hfs)
src/mess/tools/imgtool/modules/amiga.c
r0r5845
1/****************************************************************************
2
3   amiga.c
4
5   Amiga floppies
6
7****************************************************************************/
8
9
10/*****************************************************************************
11 Includes
12*****************************************************************************/
13
14
15#include <time.h>
16#include <string.h>
17#include <ctype.h>
18
19#include "imgtool.h"
20#include "iflopimg.h"
21#include "utils.h"
22#include "formats/ami_dsk.h"
23
24
25
26/*****************************************************************************
27 Data structures
28*****************************************************************************/
29
30
31#define BSIZE (512) /* Block size is always 512 bytes for floppies */
32#define TSIZE ((BSIZE/4) - 56) /* Size of data tables */
33#define MSIZE ((BSIZE/4) - 1) /* Size of bitmaps */
34
35
36typedef enum
37{
38   DT_UNKNOWN       = -1,
39   DT_OFS           =  0,
40   DT_FFS           =  1,
41   DT_OFS_INTL      =  2,
42   DT_FFS_INTL      =  3,
43   DT_OFS_INTL_DIRC =  4,
44   DT_FFS_INTL_DIRC =  5
45} disk_type;
46
47
48enum
49{
50   T_INVALID  =  0,
51   T_HEADER   =  2,
52   T_DATA     =  8,
53   T_LIST     = 16,
54   T_DIRCACHE = 33
55};
56
57
58typedef enum
59{
60   ST_INVALID  =  0,
61   ST_ROOT     =  1,
62   ST_USERDIR  =  2,
63   ST_FILE     = -3,
64   ST_LINKFILE = -4,
65   ST_LINKDIR  =  4,
66   ST_SOFTLINK =  3
67} sec_type;
68
69
70typedef struct _amiga_date amiga_date;
71struct _amiga_date
72{
73   UINT32 days;  /* days since 1 jan 78 */
74   UINT32 mins;  /* minutes past midnight */
75   UINT32 ticks; /* ticks (1/50 sec) past last minute */
76};
77
78
79typedef struct _root_block root_block;
80struct _root_block
81{
82   UINT32 ht_size;      /* Hash table size in long */
83   UINT32 chksum;       /* Rootblock checksum */
84   UINT32 ht[TSIZE];    /* Hash table (entry block number) */
85   UINT32 bm_flag;      /* bitmap flag, -1 means VALID */
86   UINT32 bm_pages[25]; /* bitmap blocks pointers (first one at bm_pages[0]) */
87   amiga_date r;        /* last root alteration date */
88   UINT8 name_len;      /* volume name length */
89   UINT8 diskname[30];  /* volume name */
90   amiga_date v;        /* last disk alteration date */
91   amiga_date c;        /* filesystem creation date */
92   UINT32 extension;    /* FFS: first directory cache block, 0 otherwise */
93};
94
95
96typedef struct _bitmap_block bitmap_block;
97struct _bitmap_block
98{
99   UINT32 chksum;      /* checksum, normal algorithm */
100   UINT32 map[MSIZE];  /* bitmap */
101};
102
103
104typedef struct _bitmap_ext_block bitmap_ext_block;
105struct _bitmap_ext_block
106{
107   UINT32 map[MSIZE];  /* bitmap */
108   UINT32 next;        /* next extension block */
109};
110
111
112typedef struct _file_block file_block;
113struct _file_block
114{
115   UINT32 header_key;   /* self pointer (to this block) */
116   UINT32 high_seq;     /* number of data block ptr stored here */
117   UINT32 first_data;   /* first data block ptr */
118   UINT32 chksum;       /* same algorithm as rootblock */
119   UINT32 data_blocks[TSIZE]; /* data blk ptr */
120   UINT16 uid;          /* UserID */
121   UINT16 gid;          /* GroupID */
122   UINT32 protect;      /* protection flags (0 by default) */
123   UINT32 byte_size;    /* file size in bytes */
124   UINT8  comm_len;     /* file comment length */
125   UINT8  comment[79];  /* comment (max. 79 chars permitted) */
126   amiga_date date;     /* last change date */
127   UINT8  name_len;     /* filename length */
128   UINT8  filename[30]; /* filename (max. 30 chars permitted) */
129   UINT32 real_entry;   /* FFS: unused, set to 0 */
130   UINT32 next_link;    /* FFS: hardlinks chained list (first == newest */
131   UINT32 hash_chain;   /* next entry ptr with same hash */
132   UINT32 parent;       /* parent directory */
133   UINT32 extension;    /* pointer to 1st file extension block */
134};
135
136
137typedef struct _file_ext_block file_ext_block;
138struct _file_ext_block
139{
140   UINT32 header_key;   /* self pointer (to this block) */
141   UINT32 high_seq;     /* number of data block ptr stored here */
142   UINT32 chksum;       /* same algorithm as rootblock */
143   UINT32 data_blocks[TSIZE]; /* data blk ptr */
144   UINT32 parent;       /* file header block */
145   UINT32 extension;    /* pointer to next file extension block */
146};
147
148
149typedef struct _data_block data_block;
150struct _data_block
151{
152   UINT32 header_key;     /* self pointer (to this block) */
153   UINT32 seq_num;        /* file data block number */
154   UINT32 data_size;      /* data size */
155   UINT32 next_data;      /* next data block ptr */
156   UINT32 chksum;         /* checksum, rootblock algorithm */
157   UINT8  data[BSIZE-24]; /* file data */
158};
159
160
161typedef struct _dir_block dir_block;
162struct _dir_block
163{
164   UINT32 header_key;   /* self pointer (to this block) */
165   UINT32 chksum;       /* same algorithm as rootblock */
166   UINT32 ht[TSIZE];    /* hash table (entry block number) */
167   UINT8  uid;          /* UserID */
168   UINT8  gid;          /* GroupID */
169   UINT32 protect;      /* protection flags (0 by default) */
170   UINT8  comm_len;     /* file comment length */
171   UINT8  comment[79];  /* comment (max. 79 chars permitted) */
172   amiga_date date;     /* last access date */
173   UINT8  name_len;     /* directory name length */
174   UINT8  dirname[30];  /* directory name (max. 30 chars permitted) */
175   UINT32 next_link;    /* FFS: hardlinks chained list (first == newest */
176   UINT32 hash_chain;   /* next entry ptr with same hash */
177   UINT32 parent;       /* parent directory */
178   UINT32 extension;    /* FFS: first directory cache block */
179};
180
181
182typedef struct _hardlink_block hardlink_block;
183struct _hardlink_block
184{
185   UINT32 header_key;   /* self pointer (to this block) */
186   UINT32 chksum;       /* same algorithm as rootblock */
187   UINT32 protect;      /* protection flags (0 by default) */
188   UINT8  comm_len;     /* file comment length */
189   UINT8  comment[79];  /* comment (max. 79 chars permitted) */
190   amiga_date date;     /* last access date */
191   UINT8  name_len;     /* hard link name length */
192   UINT8  hlname[30];   /* hard link name (max. 30 chars permitted) */
193   UINT32 real_entry;   /* FFS: pointer to "real" file or directory */
194   UINT32 next_link;    /* FFS: hardlinks chained list (first == newest */
195   UINT32 hash_chain;   /* next entry ptr with same hash */
196   UINT32 parent;       /* parent directory */
197   UINT32 sec_type;     /* secondary type, ST_LINKFILE/ST_LINKDIR */
198};
199
200
201typedef struct _softlink_block softlink_block;
202struct _softlink_block
203{
204   UINT32 header_key;   /* self pointer (to this block) */
205   UINT32 chksum;       /* same algorithm as rootblock */
206   UINT8  symbolic_name[BSIZE-224]; /* path name to referenced object */
207   UINT32 protect;      /* protection flags (0 by default) */
208   UINT8  comm_len;     /* file comment length */
209   UINT8  comment[79];  /* comment (max. 79 chars permitted) */
210   amiga_date date;     /* last access date */
211   UINT8  name_len;     /* soft link name length */
212   UINT8  slname[30];   /* soft link name (max. 30 chars permitted) */
213   UINT32 hash_chain;   /* next entry ptr with same hash */
214   UINT32 parent;       /* parent directory */
215};
216
217
218/* Basic Amiga floppy disk image info */
219typedef struct _amiga_floppy amiga_floppy;
220struct _amiga_floppy
221{
222   imgtool_stream *stream;
223   UINT8 sectors;
224};
225
226
227/* iterator used to walk through directory entries */
228typedef struct _amiga_iterator amiga_iterator;
229struct _amiga_iterator
230{
231   unsigned int index;    /* current file index */
232   int block;             /* block number we are iterating */
233   UINT32 next_block;     /* next block in hash chain */
234   int ht_index;          /* current index in the hash table */
235   unsigned int eof : 1;  /* end of file listing reached? */
236};
237
238
239/*****************************************************************************
240 Prototypes
241*****************************************************************************/
242
243
244static imgtoolerr_t amiga_image_read_sector(imgtool_image* img,
245   UINT32 track, UINT32 head, UINT32 sector, void *buf, size_t len);
246static imgtoolerr_t amiga_image_write_sector(imgtool_image* img,
247   UINT32 track, UINT32 head, UINT32 sector, const void *buf, size_t len, int ddam);
248
249
250
251/*****************************************************************************
252 Utility functions
253*****************************************************************************/
254
255
256/* Amiga version of the toupper function */
257static int intl_toupper(int c)
258{
259   return (c>='a' && c<='z') || (c>=224 && c<=254 && c!=247) ? c - ('a'-'A') : c ;
260}
261
262
263/* Amiga filename case insensitive string compare */
264static int intl_stricmp(const char *s1, const char *s2)
265{
266   for (;;)
267    {
268      int c1 = intl_toupper(*s1++);
269      int c2 = intl_toupper(*s2++);
270
271      if (c1 == 0 || c1 != c2)
272         return c1 - c2;
273    }
274}
275
276
277/* Calculate the hash value for a filename */
278static int hash_name(const char *name, int intl)
279{
280   int i, l = strlen(name);
281   UINT32 hash = l;
282
283   for(i = 0; i < l; i++)
284   {
285      hash *= 13;
286      hash += (UINT8) (intl ? intl_toupper(name[i]) : toupper(name[i]));
287      hash &= 0x7ff;
288   }
289
290   return hash % TSIZE; /* 0 < hash < 71 in case of BSIZE=512 */
291}
292
293
294/* Returns TRUE if year is a leap year */
295int is_leap(int year)
296{
297   return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
298}
299
300
301/* Convert amiga time to standard time */
302static time_t amiga_crack_time(amiga_date *date)
303{
304    int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
305   int year = 1978, month = 1, year_days = 365; /* base date */
306   int day = date->days;
307   struct tm t;
308
309   /* initialize struct */
310   memset(&t, 0, sizeof(t));
311
312    /* first calculate the year */
313    while (day >= year_days)
314    {
315        day -= year_days;
316        year_days = is_leap(++year) ? 366 : 365;
317    }
318
319    /* then the month */
320    if (is_leap(year))
321      month_days[1] = 29;
322
323    while(day >= month_days[month-1])
324    {
325        day -= month_days[month-1];
326        month++;
327    }
328
329   /* fill the struct with our calculated values */
330    t.tm_year = year - 1900;
331    t.tm_mon  = month - 1;
332    t.tm_mday = day + 1;
333    t.tm_hour = date->mins / 60;
334   t.tm_min  = date->mins % 60;
335   t.tm_sec  = date->ticks / 50;
336
337   return mktime(&t);
338}
339
340
341/* convert standard time to amiga time */
342void amiga_setup_time(time_t time, amiga_date *dest)
343{
344   struct tm t = *localtime(&time);
345   int year;
346
347   dest->days = 0;
348
349   for (year = 1978; year < t.tm_year + 1900; year++)
350   {
351      dest->days += is_leap(year) ? 366 : 365;
352   }
353
354   dest->days += t.tm_yday;
355   dest->mins = t.tm_hour * 60 + t.tm_min;
356   dest->ticks = t.tm_sec * 50;
357}
358
359
360/* convert flags to human readable form */
361void amiga_decode_flags(UINT32 flags, char *dest)
362{
363   /* test for flags */
364   dest[0] = (flags & 0x80) ? 'h' : '-';
365   dest[1] = (flags & 0x40) ? 's' : '-';
366   dest[2] = (flags & 0x20) ? 'p' : '-';
367   dest[3] = (flags & 0x10) ? 'a' : '-';
368   dest[4] = (flags & 0x08) ? '-' : 'r';
369   dest[5] = (flags & 0x04) ? '-' : 'w';
370   dest[6] = (flags & 0x02) ? '-' : 'e';
371   dest[7] = (flags & 0x01) ? '-' : 'd';
372   dest[8] = '\0';
373}
374
375
376void copy_integer_array_be(UINT32 *dest, const UINT32 *source, int size)
377{
378   int i;
379
380   for (i = 0; i < size; i++)
381   {
382      dest[i] = BIG_ENDIANIZE_INT32(source[i]);
383   }
384}
385
386
387/* This function converts an array of UINT32s to an amiga_date */
388void copy_date_be(amiga_date *dest, const UINT32 *source)
389{
390   dest->days  = BIG_ENDIANIZE_INT32(source[0]);
391   dest->mins  = BIG_ENDIANIZE_INT32(source[1]);
392   dest->ticks = BIG_ENDIANIZE_INT32(source[2]);
393}
394
395
396/* Calculate the block checksum of a byte array */
397UINT32 block_checksum(UINT8 *buffer, int length)
398{
399   UINT32 chksum = 0;
400   int i;
401
402   for (i = 0; i < length/4; i++)
403   {
404      chksum += pick_integer_be(buffer, i*4, 4);
405   }
406
407   return -chksum;
408}
409
410
411/* Returns the total number of blocks in the image */
412static int get_total_blocks(imgtool_image *img)
413{
414   amiga_floppy *f = imgtool_image_extra_bytes(img);
415
416   return 2 * 80 * f->sectors;
417}
418
419
420/* Returns track, head and sector for a block */
421static void find_block(amiga_floppy *f, int block, int *track,
422   int *head, int *sector)
423{
424   *track = block / f->sectors;
425   *head = (block - *track * f->sectors) / f->sectors;
426   *sector = (block - *track * f->sectors) % f->sectors;
427}
428
429
430/* Generic read block */
431static imgtoolerr_t read_block(imgtool_image *img, int block, UINT8 *buffer)
432{
433   imgtoolerr_t ret;
434   int track, head, sector;
435
436   find_block((amiga_floppy *) imgtool_image_extra_bytes(img), block, &track, &head, &sector);
437
438   /* get block from image */
439   ret = amiga_image_read_sector(img, track, head, sector, buffer, BSIZE);
440   if (ret) return ret;
441
442   return IMGTOOLERR_SUCCESS;
443}
444
445
446/* Generic write block */
447static imgtoolerr_t write_block(imgtool_image *img, int block, const UINT8 *buffer)
448{
449   imgtoolerr_t ret;
450   int track, head, sector;
451
452   find_block((amiga_floppy *) imgtool_image_extra_bytes(img), block, &track, &head, &sector);
453
454   /* write block to image */
455   ret = amiga_image_write_sector(img, track, head, sector, buffer, BSIZE, 0);
456   if (ret) return ret;
457
458   return IMGTOOLERR_SUCCESS;
459}
460
461
462/* Return the type a block */
463static sec_type get_block_type(imgtool_image *img, int block)
464{
465   imgtoolerr_t ret;
466   UINT8 buffer[BSIZE];
467
468   /* get data */
469   ret = read_block(img, block, buffer);
470   if (ret) return ST_INVALID;
471
472   /* return type */
473   switch ((INT32) pick_integer_be(buffer, BSIZE-4, 4))
474   {
475   case  1: return ST_ROOT;
476   case  2: return ST_USERDIR;
477   case -3: return ST_FILE;
478   case -4: return ST_LINKFILE;
479   case  4: return ST_LINKDIR;
480   case  3: return ST_SOFTLINK;
481   default: return ST_INVALID;
482   }
483}
484
485
486/* Read a bitmap block */
487static imgtoolerr_t read_bitmap_block(imgtool_image *img, int block, bitmap_block *bm)
488{
489   imgtoolerr_t ret;
490   UINT8 buffer[BSIZE];
491
492   /* read block */
493   ret = read_block(img, block, buffer);
494   if (ret) return ret;
495
496   /* fill in data */
497   bm->chksum = pick_integer_be(buffer, 0, 4);
498   copy_integer_array_be(bm->map, (UINT32 *) &buffer[4], MSIZE);
499
500   return IMGTOOLERR_SUCCESS;
501}
502
503
504/* Write a bitmap block */
505static imgtoolerr_t write_bitmap_block(imgtool_image *img, int block, const bitmap_block *bm)
506{
507   imgtoolerr_t ret;
508   UINT8 buffer[BSIZE];
509
510   /* Setup buffer */
511   place_integer_be(buffer, 0, 4, bm->chksum);
512   copy_integer_array_be((UINT32 *) &buffer[4], bm->map, MSIZE);
513
514   /* write block */
515   ret = write_block(img, block, buffer);
516   if (ret) return ret;
517
518   return IMGTOOLERR_SUCCESS;
519}
520
521
522/* Read a bitmap extended block */
523static imgtoolerr_t read_bitmap_ext_block(imgtool_image *img, int block, bitmap_ext_block *bm)
524{
525   imgtoolerr_t ret;
526   UINT8 buffer[BSIZE];
527
528   /* read block */
529   ret = read_block(img, block, buffer);
530   if (ret) return ret;
531
532   /* fill in data */
533   copy_integer_array_be(bm->map, (UINT32 *) &buffer, MSIZE);
534   bm->next = pick_integer_be(buffer, BSIZE-4, 4);
535
536   return IMGTOOLERR_SUCCESS;
537}
538
539
540/* Read the root block */
541static imgtoolerr_t read_root_block(imgtool_image *img, root_block *root)
542{
543   imgtoolerr_t ret;
544   UINT8 buffer[BSIZE];
545
546   /* get raw root block from image */
547   ret = read_block(img, get_total_blocks(img)/2, buffer);
548   if (ret) return ret;
549
550   /* copy data to root_block */
551   memset(root, 0, sizeof(root_block));
552
553   root->ht_size = pick_integer_be(buffer, 12, 4);
554   root->chksum = pick_integer_be(buffer, 20, 4);
555   copy_integer_array_be(root->ht, (UINT32 *) &buffer[24], TSIZE);
556   root->bm_flag = pick_integer_be(buffer, BSIZE-200, 4);
557   copy_integer_array_be(root->bm_pages, (UINT32 *) &buffer[BSIZE-196], 25);
558   copy_date_be(&root->r, (UINT32 *) &buffer[BSIZE-92]);
559   root->name_len = pick_integer_be(buffer, BSIZE-80, 1);
560   memcpy(root->diskname, &buffer[BSIZE-79], 30);
561   copy_date_be(&root->v, (UINT32 *) &buffer[BSIZE-40]);
562   copy_date_be(&root->c, (UINT32 *) &buffer[BSIZE-28]);
563   root->extension = pick_integer_be(buffer, BSIZE-8, 4);
564
565   return IMGTOOLERR_SUCCESS;
566}
567
568
569static imgtoolerr_t write_root_block(imgtool_image *img, const root_block *root)
570{
571   imgtoolerr_t ret;
572   UINT8 buffer[BSIZE];
573
574   /* Setup buffer */
575   memset(buffer, 0, BSIZE);
576
577   place_integer_be(buffer, 0, 4, T_HEADER);
578   place_integer_be(buffer, 12, 4, root->ht_size);
579   place_integer_be(buffer, 20, 4, root->chksum);
580   copy_integer_array_be((UINT32 *) &buffer[24], root->ht, TSIZE);
581   place_integer_be(buffer, BSIZE-200, 4, root->bm_flag);
582   copy_integer_array_be((UINT32 *) &buffer[BSIZE-196], root->bm_pages, 25);
583   place_integer_be(buffer, BSIZE-92, 4, root->r.days);
584   place_integer_be(buffer, BSIZE-88, 4, root->r.mins);
585   place_integer_be(buffer, BSIZE-84, 4, root->r.ticks);
586   place_integer_be(buffer, BSIZE-80, 1, root->name_len);
587   memcpy(&buffer[BSIZE-79], root->diskname, root->name_len);
588   place_integer_be(buffer, BSIZE-40, 4, root->v.days);
589   place_integer_be(buffer, BSIZE-36, 4, root->v.mins);
590   place_integer_be(buffer, BSIZE-32, 4, root->v.ticks);
591   place_integer_be(buffer, BSIZE-28, 4, root->c.days);
592   place_integer_be(buffer, BSIZE-24, 4, root->c.mins);
593   place_integer_be(buffer, BSIZE-20, 4, root->c.ticks);
594   place_integer_be(buffer, BSIZE-8, 4, root->extension);
595   place_integer_be(buffer, BSIZE-4, 4, ST_ROOT);
596
597   /* write root block to image */
598   ret = write_block(img, get_total_blocks(img)/2, buffer);
599   if (ret) return ret;
600
601   return IMGTOOLERR_SUCCESS;
602}
603
604
605/* Read a file block */
606static imgtoolerr_t read_file_block(imgtool_image *img, int block, file_block *fb)
607{
608   imgtoolerr_t ret;
609   UINT8 buffer[BSIZE];
610
611   /* read block */
612   ret = read_block(img, block, buffer);
613   if (ret) return ret;
614
615   /* fill in data */
616   fb->header_key = pick_integer_be(buffer, 4, 4);
617   fb->high_seq = pick_integer_be(buffer, 8, 4);
618   fb->first_data = pick_integer_be(buffer, 16, 4);
619   fb->chksum = pick_integer_be(buffer, 20, 4);
620   copy_integer_array_be(fb->data_blocks, (UINT32 *) &buffer[24], TSIZE);
621   fb->uid = pick_integer_be(buffer, BSIZE-196, 2);
622   fb->gid = pick_integer_be(buffer, BSIZE-194, 2);
623   fb->protect = pick_integer_be(buffer, BSIZE-192, 4);
624   fb->byte_size = pick_integer_be(buffer, BSIZE-188, 4);
625   fb->comm_len = pick_integer_be(buffer, BSIZE-184, 1);
626   memcpy(fb->comment, &buffer[BSIZE-183], 79);
627   copy_date_be(&fb->date, (UINT32 *) &buffer[BSIZE-92]);
628   fb->name_len = pick_integer_be(buffer, BSIZE-80, 1);
629   memcpy(fb->filename, (UINT32 *) &buffer[BSIZE-79], 30);
630   fb->real_entry = pick_integer_be(buffer, BSIZE-44, 4);
631   fb->next_link = pick_integer_be(buffer, BSIZE-40, 4);
632   fb->hash_chain = pick_integer_be(buffer, BSIZE-16, 4);
633   fb->parent = pick_integer_be(buffer, BSIZE-12, 4);
634   fb->extension = pick_integer_be(buffer, BSIZE-8, 4);
635
636   return IMGTOOLERR_SUCCESS;
637}
638
639
640static imgtoolerr_t read_file_ext_block(imgtool_image *img, int block, file_ext_block *fe)
641{
642   imgtoolerr_t ret;
643   UINT8 buffer[BSIZE];
644
645   /* read block */
646   ret = read_block(img, block, buffer);
647   if (ret) return ret;
648
649   /* fill in data */
650   fe->header_key = pick_integer_be(buffer, 4, 4);
651   fe->high_seq = pick_integer_be(buffer, 8, 4);
652   fe->chksum = pick_integer_be(buffer, 20, 4);
653   copy_integer_array_be(fe->data_blocks, (UINT32 *) &buffer[24], TSIZE);
654   fe->parent = pick_integer_be(buffer, BSIZE-12, 4);
655   fe->extension = pick_integer_be(buffer, BSIZE-8, 4);
656
657   return IMGTOOLERR_SUCCESS;
658}
659
660
661static imgtoolerr_t read_data_block(imgtool_image *img, int block, data_block *d)
662{
663   imgtoolerr_t ret;
664   UINT8 buffer[BSIZE];
665
666   /* read block */
667   ret = read_block(img, block, buffer);
668   if (ret) return ret;
669
670   /* fill in data */
671   d->header_key = pick_integer_be(buffer, 4, 4);
672   d->seq_num = pick_integer_be(buffer, 8, 4);
673   d->data_size = pick_integer_be(buffer, 12, 4);
674   d->next_data = pick_integer_be(buffer, 16, 4);
675   d->chksum = pick_integer_be(buffer, 20, 4);
676   memcpy(d->data, &buffer[24], BSIZE-24);
677
678   return IMGTOOLERR_SUCCESS;
679}
680
681
682/* Read a directory block */
683static imgtoolerr_t read_dir_block(imgtool_image *img, int block, dir_block *db)
684{
685   imgtoolerr_t ret;
686   UINT8 buffer[BSIZE];
687
688   /* read block */
689   ret = read_block(img, block, buffer);
690   if (ret) return ret;
691
692   /* fill in data */
693   db->header_key = pick_integer_be(buffer, 4, 4);
694   db->chksum = pick_integer_be(buffer, 20, 4);
695   copy_integer_array_be(db->ht, (UINT32 *) &buffer[24], TSIZE);
696   db->uid = pick_integer_be(buffer, BSIZE-196, 2);
697   db->gid = pick_integer_be(buffer, BSIZE-194, 2);
698   db->protect = pick_integer_be(buffer, BSIZE-192, 4);
699   db->comm_len = pick_integer_be(buffer, BSIZE-184, 1);
700   memcpy(db->comment, &buffer[BSIZE-183], 79);
701   copy_date_be(&db->date, (UINT32 *) &buffer[BSIZE-92]);
702   db->name_len = pick_integer_be(buffer, BSIZE-80, 1);
703   memcpy(db->dirname, (UINT32 *) &buffer[BSIZE-79], 30);
704   db->next_link = pick_integer_be(buffer, BSIZE-40, 4);
705   db->hash_chain = pick_integer_be(buffer, BSIZE-16, 4);
706   db->parent = pick_integer_be(buffer, BSIZE-12, 4);
707   db->extension = pick_integer_be(buffer, BSIZE-8, 4);
708
709   return IMGTOOLERR_SUCCESS;
710}
711
712
713static imgtoolerr_t write_dir_block(imgtool_image *img, int block, const dir_block *db)
714{
715   UINT8 buffer[BSIZE];
716
717   /* Setup buffer */
718   memset(buffer, 0, BSIZE);
719
720   /* Copy data */
721   place_integer_be(buffer, 0, 4, T_HEADER);
722   place_integer_be(buffer, 4, 4, db->header_key);
723   place_integer_be(buffer, 20, 4, db->chksum);
724   copy_integer_array_be((UINT32 *) &buffer[24], db->ht, TSIZE);
725   place_integer_be(buffer, BSIZE-196, 2, db->uid);
726   place_integer_be(buffer, BSIZE-194, 2, db->gid);
727   place_integer_be(buffer, BSIZE-192, 4, db->protect);
728   place_integer_be(buffer, BSIZE-184, 1, db->comm_len);
729   memcpy((UINT32 *) &buffer[BSIZE-183], db->comment, db->comm_len);
730   place_integer_be(buffer, BSIZE-92, 4, db->date.days);
731   place_integer_be(buffer, BSIZE-88, 4, db->date.mins);
732   place_integer_be(buffer, BSIZE-84, 4, db->date.ticks);
733   place_integer_be(buffer, BSIZE-80, 1, db->name_len);
734   memcpy((UINT32 *) &buffer[BSIZE-79], db->dirname, db->name_len);
735   place_integer_be(buffer, BSIZE-40, 4, db->next_link);
736   place_integer_be(buffer, BSIZE-16, 4, db->hash_chain);
737   place_integer_be(buffer, BSIZE-12, 4, db->parent);
738   place_integer_be(buffer, BSIZE-8, 4, db->extension);
739   place_integer_be(buffer, BSIZE-4, 4, ST_USERDIR);
740
741   /* Write block to disk */
742   return write_block(img, block, buffer);
743}
744
745
746static imgtoolerr_t read_hardlink_block(imgtool_image *img, int block, hardlink_block *hl)
747{
748   imgtoolerr_t ret;
749   UINT8 buffer[BSIZE];
750
751   /* read block */
752   ret = read_block(img, block, buffer);
753   if (ret) return ret;
754
755   /* fill in data */
756   hl->header_key = pick_integer_be(buffer, 4, 4);
757   hl->chksum = pick_integer_be(buffer, 20, 4);
758   hl->protect = pick_integer_be(buffer, BSIZE-192, 4);
759   hl->comm_len = pick_integer_be(buffer, BSIZE-184, 1);
760   memcpy(hl->comment, &buffer[BSIZE-183], 79);
761   copy_date_be(&hl->date, (UINT32 *) &buffer[BSIZE-92]);
762   hl->name_len = pick_integer_be(buffer, BSIZE-80, 1);
763   memcpy(hl->hlname, (UINT32 *) &buffer[BSIZE-79], 30);
764   hl->real_entry = pick_integer_be(buffer, BSIZE-44, 4);
765   hl->next_link = pick_integer_be(buffer, BSIZE-40, 4);
766   hl->hash_chain = pick_integer_be(buffer, BSIZE-16, 4);
767   hl->parent = pick_integer_be(buffer, BSIZE-12, 4);
768   hl->sec_type = pick_integer_be(buffer, BSIZE-4, 4);
769
770   return IMGTOOLERR_SUCCESS;
771}
772
773
774static imgtoolerr_t read_softlink_block(imgtool_image *img, int block, softlink_block *sl)
775{
776   imgtoolerr_t ret;
777   UINT8 buffer[BSIZE];
778
779   /* read block */
780   ret = read_block(img, block, buffer);
781   if (ret) return ret;
782
783   /* fill in data */
784   sl->header_key = pick_integer_be(buffer, 4, 4);
785   sl->chksum = pick_integer_be(buffer, 20, 4);
786   memcpy(sl->symbolic_name, &buffer[24], BSIZE-224);
787   sl->protect = pick_integer_be(buffer, BSIZE-192, 4);
788   sl->comm_len = pick_integer_be(buffer, BSIZE-184, 1);
789   memcpy(sl->comment, &buffer[BSIZE-183], 79);
790   copy_date_be(&sl->date, (UINT32 *) &buffer[BSIZE-92]);
791   sl->name_len = pick_integer_be(buffer, BSIZE-80, 1);
792   memcpy(sl->slname, (UINT32 *) &buffer[BSIZE-79], 30);
793   sl->hash_chain = pick_integer_be(buffer, BSIZE-16, 4);
794   sl->parent = pick_integer_be(buffer, BSIZE-12, 4);
795
796   return IMGTOOLERR_SUCCESS;
797}
798
799
800/* Returns the disk type */
801static disk_type get_disk_type(imgtool_image *img)
802{
803   imgtoolerr_t ret;
804   UINT8 buffer[BSIZE];
805
806   ret = read_block(img, 0, buffer);
807   if (ret) return DT_UNKNOWN;
808
809   switch(buffer[3])
810   {
811   case  0: return DT_OFS;
812   case  1: return DT_FFS;
813   case  2: return DT_OFS_INTL;
814   case  3: return DT_FFS_INTL;
815   case  4: return DT_OFS_INTL_DIRC;
816   case  5: return DT_FFS_INTL_DIRC;
817   default: return DT_UNKNOWN;
818   }
819}
820
821
822/* Returns TRUE if the disk is formatted with the FastFileSystem */
823static int is_ffs(imgtool_image *img)
824{
825   disk_type t = get_disk_type(img);
826
827   return ((t == DT_FFS ||
828          t == DT_FFS_INTL ||
829          t == DT_FFS_INTL_DIRC) ? TRUE : FALSE);
830}
831
832
833/* Returns TRUE if the disk uses the international mode */
834static int is_intl(imgtool_image *img)
835{
836   disk_type t = get_disk_type(img);
837
838   return ((t == DT_OFS_INTL ||
839          t == DT_FFS_INTL ||
840          t == DT_OFS_INTL_DIRC ||
841          t == DT_FFS_INTL_DIRC) ? TRUE : FALSE);
842}
843
844#ifdef UNUSED_FUNCTION
845/* Returns TRUE if the disk uses the directory cache mode */
846static int is_dirc(imgtool_image *img)
847{
848   disk_type t = get_disk_type(img);
849
850   return ((t == DT_OFS_INTL_DIRC ||
851          t == DT_FFS_INTL_DIRC) ? TRUE : FALSE);
852}
853#endif
854
855static imgtoolerr_t get_hash_table(imgtool_image *img, int block, UINT32 *ht)
856{
857   imgtoolerr_t ret;
858
859   switch (get_block_type(img, block))
860   {
861   case ST_USERDIR:
862   {
863      dir_block dir;
864
865      /* get the directory block */
866      ret = read_dir_block(img, block, &dir);
867      if (ret) return ret;
868
869      /* copy data */
870      memcpy(ht, &dir.ht, TSIZE*4);
871
872      return IMGTOOLERR_SUCCESS;
873   }
874
875   case ST_ROOT:
876   {
877      root_block root;
878
879      /* get the root block */
880      ret = read_root_block(img, &root);
881      if (ret) return ret;
882
883      /* copy data */
884      memcpy(ht, &root.ht, TSIZE*4);
885
886      return IMGTOOLERR_SUCCESS;
887   }
888
889   default:
890      return IMGTOOLERR_UNEXPECTED;
891   }
892}
893
894
895static imgtoolerr_t set_hash_table(imgtool_image *img, int block, const UINT32 *ht)
896{
897   UINT8 buffer[BSIZE];
898   imgtoolerr_t ret;
899
900   /* Read block */
901   ret = read_block(img, block, buffer);
902   if (ret) return ret;
903
904   /* Copy new hash table into it */
905   copy_integer_array_be((UINT32 *) &buffer[24], ht, TSIZE);
906
907   /* Write it back again */
908   ret = write_block(img, block, buffer);
909   if (ret) return ret;
910
911   return IMGTOOLERR_SUCCESS;
912}
913
914#ifdef UNUSED_FUNCTION
915static imgtoolerr_t get_root_hash_table(imgtool_image *img, UINT32 *ht)
916{
917   return get_hash_table(img, get_total_blocks(img)/2, ht);
918}
919#endif
920
921static imgtoolerr_t get_blockname(imgtool_image *img, int block, char *dest)
922{
923   UINT8 buffer[BSIZE];
924   imgtoolerr_t ret;
925
926   /* Read the block */
927   ret = read_block(img, block, buffer);
928   if (ret) return ret;
929
930   /* Copy filename out of the buffer */
931   memset(dest, 0, 31);
932   memcpy(dest, &buffer[BSIZE-79], buffer[BSIZE-80]);
933
934   return IMGTOOLERR_SUCCESS;
935}
936
937
938static imgtoolerr_t get_hash_chain(imgtool_image *img, int block, UINT32 *chain)
939{
940   UINT8 buffer[BSIZE];
941   imgtoolerr_t ret;
942
943   /* Read block */
944   ret = read_block(img, block, buffer);
945   if (ret) return ret;
946
947   /* Get chain value */
948   *chain = pick_integer_be(buffer, BSIZE-16, 4);
949
950   return IMGTOOLERR_SUCCESS;
951}
952
953
954static imgtoolerr_t set_hash_chain(imgtool_image *img, int block, UINT32 chain)
955{
956   UINT8 buffer[BSIZE];
957   imgtoolerr_t ret;
958
959   /* Read block */
960   ret = read_block(img, block, buffer);
961   if (ret) return ret;
962
963   /* Copy new hash chain value into it */
964   place_integer_be(buffer, BSIZE-16, 4, chain);
965
966   /* Write it back again */
967   ret = write_block(img, block, buffer);
968   if (ret) return ret;
969
970   return IMGTOOLERR_SUCCESS;
971}
972
973
974static imgtoolerr_t walk_hash_chain(imgtool_image *img, const char *path, int start_block, int *prev_block, int *block)
975{
976   imgtoolerr_t err;
977   UINT32 hash_chain;
978   char name[31];
979
980   /* choose compare function depending on intl mode */
981   int (*cmp)(const char *, const char *) = is_intl(img) ? &intl_stricmp : &mame_stricmp;
982
983   /* initialize filenames */
984   memset(name, 0, sizeof(name));
985
986   switch (get_block_type(img, start_block))
987   {
988   case ST_USERDIR:
989   {
990      dir_block dir;
991
992      /* read block */
993      err = read_dir_block(img, start_block, &dir);
994      if (err) return err;
995
996      /* copy filename string and next hash */
997      memcpy(name, dir.dirname, dir.name_len);
998      hash_chain = dir.hash_chain;
999
1000      break;
1001   }
1002
1003   case ST_FILE:
1004   {
1005      file_block file;
1006
1007      /* read block */
1008      err = read_file_block(img, start_block, &file);
1009      if (err) return err;
1010
1011      /* copy filename string and next hash */
1012      memcpy(name, file.filename, file.name_len);
1013      hash_chain = file.hash_chain;
1014
1015      break;
1016   }
1017
1018   case ST_SOFTLINK:
1019   {
1020      softlink_block sl;
1021
1022      /* read block */
1023      err = read_softlink_block(img, start_block, &sl);
1024      if (err) return err;
1025
1026      /* copy filename string and next hash */
1027      memcpy(name, sl.slname, sl.name_len);
1028      hash_chain = sl.hash_chain;
1029
1030      break;
1031   }
1032
1033   case ST_LINKDIR:
1034   case ST_LINKFILE:
1035   {
1036      hardlink_block hl;
1037
1038      /* read block */
1039      err = read_hardlink_block(img, start_block, &hl);
1040      if (err) return err;
1041
1042      /* copy filename string and next hash */
1043      memcpy(name, hl.hlname, hl.name_len);
1044      hash_chain = hl.hash_chain;
1045      break;
1046   }
1047
1048   default:
1049      return IMGTOOLERR_UNEXPECTED;
1050
1051   }
1052
1053   /* if we haven't found the right filename but there are linked entries,
1054    * walk up the chain */
1055   if ((*cmp)(name, path) != 0 && hash_chain != 0)
1056   {
1057      *prev_block = start_block;
1058      return walk_hash_chain(img, path, hash_chain, prev_block, block);
1059   }
1060
1061   /* found the correct block, return */
1062   if ((*cmp)(name, path) == 0)
1063   {
1064      *block = start_block;
1065      return IMGTOOLERR_SUCCESS;
1066   }
1067
1068   /* we should never get here */
1069   return IMGTOOLERR_UNEXPECTED;
1070}
1071
1072
1073/* Returns the block number for a dir/file/link entry given as NUL delimited
1074 * list of path parts, for example "dir1\0dir2\0dir3" returns the block number
1075 * for directory "dir3" */
1076static imgtoolerr_t find_entry(imgtool_image *img, const char *path, int start_block, int *block)
1077{
1078   imgtoolerr_t ret;
1079   const char *next_path;
1080   int current_block, prev;
1081   UINT32 ht[TSIZE];
1082
1083   /* get the hash table */
1084   ret = get_hash_table(img, start_block, ht);
1085   if (ret) return ret;
1086
1087   /* calculate hash and get block for initial entry */
1088   current_block = ht[hash_name(path, is_intl(img))];
1089
1090   /* check if there was a match in the hash table */
1091   if (current_block == 0)
1092   {
1093      return IMGTOOLERR_PATHNOTFOUND;
1094   }
1095
1096   /* walk the linked hash list */
1097   ret = walk_hash_chain(img, path, current_block, &prev, block);
1098   if (ret) return ret;
1099
1100   /* follow links */
1101   switch (get_block_type(img, *block))
1102   {
1103   case ST_SOFTLINK:
1104
1105      /* TODO: Softlink support */
1106      return IMGTOOLERR_UNIMPLEMENTED;
1107
1108      break;
1109
1110   case ST_LINKDIR:
1111   case ST_LINKFILE:
1112   {
1113      hardlink_block hl;
1114
1115      ret = read_hardlink_block(img, *block, &hl);
1116      if (ret) return ret;
1117
1118      *block = hl.real_entry;
1119
1120      break;
1121   }
1122
1123   default:
1124      break;
1125   }
1126
1127   /* get next path part */
1128   next_path = path + strlen(path) + 1;
1129
1130   /* if there are more path parts, search the next block */
1131   if (next_path[0])
1132   {
1133      return find_entry(img, next_path, *block, block);
1134   }
1135
1136   return IMGTOOLERR_SUCCESS;
1137}
1138
1139
1140static imgtoolerr_t get_block_chksum(imgtool_image *img, int block, UINT32 *chksum, int bitmap)
1141{
1142   imgtoolerr_t ret;
1143   UINT8 buffer[BSIZE];
1144
1145   /* get block data */
1146   ret = read_block(img, block, buffer);
1147   if (ret) return ret;
1148
1149   /* clear old checksum */
1150   if (bitmap)
1151   {
1152      memset(buffer, 0, 4);
1153   }
1154   else
1155   {
1156      memset(&buffer[20], 0, 4);
1157   }
1158
1159   /* calulate checksum */
1160   *chksum = block_checksum(buffer, BSIZE);
1161
1162   return IMGTOOLERR_SUCCESS;
1163}
1164
1165
1166static imgtoolerr_t fix_chksum(imgtool_image *img, int block, int bitmap)
1167{
1168   imgtoolerr_t ret;
1169   UINT8 buffer[BSIZE];
1170   UINT32 chksum;
1171
1172   /* calculate block checksum */
1173   ret = get_block_chksum(img, block, &chksum, bitmap);
1174   if (ret) return ret;
1175
1176   /* read block data */
1177   ret = read_block(img, block, buffer);
1178   if (ret) return ret;
1179
1180   /* update checksum */
1181   if (bitmap)
1182   {
1183      place_integer_be(buffer, 0, 4, chksum);
1184   }
1185   else
1186   {
1187      place_integer_be(buffer, 20, 4, chksum);
1188   }
1189
1190   /* write back new block data */
1191   ret = write_block(img, block, buffer);
1192   if (ret) return ret;
1193
1194   return IMGTOOLERR_SUCCESS;
1195}
1196
1197
1198static imgtoolerr_t fix_block_chksum(imgtool_image *img, int block)
1199{
1200   return fix_chksum(img, block, FALSE);
1201}
1202
1203
1204static imgtoolerr_t fix_bitmap_chksum(imgtool_image *img, int block)
1205{
1206   return fix_chksum(img, block, TRUE);
1207}
1208
1209
1210/* Set a block as used */
1211static imgtoolerr_t bitmap_mark(imgtool_image *img, int block, int used)
1212{
1213   imgtoolerr_t ret;
1214   bitmap_block bm;
1215   root_block root;
1216   int page;
1217
1218   block -= 2; /* subtract boot block sectors, 2 only for floppies! */
1219
1220   ret = read_root_block(img, &root);
1221   if (ret) return ret;
1222
1223   /* figure out bitmap block location */
1224   page = root.bm_pages[block / (MSIZE * 32)];
1225
1226   /* get bitmap */
1227   ret = read_bitmap_block(img, page, &bm);
1228   if (ret) return ret;
1229
1230   /* subtract pages we skip */
1231   block -= MSIZE * 32 * (block / (MSIZE * 32));
1232
1233   /* mark as used or free */
1234   if (used)
1235   {
1236      bm.map[block/32] &= ~(1 << block % 32);
1237   }
1238   else
1239   {
1240      bm.map[block/32] |= (1 << block % 32);
1241   }
1242
1243   /* write changed bitmap block back to disk */
1244   ret = write_bitmap_block(img, page, &bm);
1245   if (ret) return ret;
1246
1247   /* update checksum */
1248   ret = fix_bitmap_chksum(img, page);
1249   if (ret) return ret;
1250
1251   return IMGTOOLERR_SUCCESS;
1252}
1253
1254
1255static imgtoolerr_t bitmap_mark_used(imgtool_image *img, int block)
1256{
1257   return bitmap_mark(img, block, TRUE);
1258}
1259
1260
1261static imgtoolerr_t bitmap_mark_free(imgtool_image *img, int block)
1262{
1263   return bitmap_mark(img, block, FALSE);
1264}
1265
1266
1267static imgtoolerr_t update_block_modified_date(imgtool_image *img, int block)
1268{
1269   UINT8 buffer[BSIZE];
1270   imgtoolerr_t ret;
1271   amiga_date date;
1272   time_t now;
1273
1274   ret = read_block(img, block, buffer);
1275   if (ret) return ret;
1276
1277   /* Set new time */
1278   time(&now);
1279   amiga_setup_time(now, &date);
1280
1281   /* Write new time into block */
1282   place_integer_be(buffer, BSIZE-92, 4, date.days);
1283   place_integer_be(buffer, BSIZE-88, 4, date.mins);
1284   place_integer_be(buffer, BSIZE-84, 4, date.ticks);
1285
1286   /* Write block back to disk */
1287   ret = write_block(img, block, buffer);
1288   if (ret) return ret;
1289
1290   return IMGTOOLERR_SUCCESS;
1291}
1292
1293
1294static imgtoolerr_t clear_hash_table_entry(imgtool_image *img, int parent, char *filename)
1295{
1296   imgtoolerr_t ret;
1297   UINT32 ht[TSIZE], chain;
1298   int index, entry, prev, block;
1299
1300   ret = get_hash_table(img, parent, ht);
1301   if (ret) return ret;
1302
1303   /* Calculate hash and get block for initial entry */
1304   index = hash_name(filename, is_intl(img));
1305   entry = ht[index];
1306
1307   /* Walk the hash chain to get the real entry */
1308   ret = walk_hash_chain(img, filename, entry, &prev, &block);
1309   if (ret) return ret;
1310
1311   /* Get chained value from block */
1312   ret = get_hash_chain(img, block, &chain);
1313   if (ret) return ret;
1314
1315   /* Check if we need to change the hash table */
1316   if (entry == block)
1317   {
1318      /* Set new value (might be 0 if there were no linked entries) */
1319      ht[index] = chain;
1320
1321      /* Save changed hash table */
1322      ret = set_hash_table(img, parent, ht);
1323      if (ret) return ret;
1324
1325      /* Update last modified date */
1326      ret = update_block_modified_date(img, parent);
1327      if (ret) return ret;
1328
1329      /* Calculate new checksum */
1330      ret = fix_block_chksum(img, parent);
1331      if (ret) return ret;
1332   }
1333   else
1334   {
1335      /* Save chained value to previous chain element */
1336      ret = set_hash_chain(img, prev, chain);
1337      if (ret) return ret;
1338
1339      /* Calculate new checksum */
1340      ret = fix_block_chksum(img, prev);
1341      if (ret) return ret;
1342   }
1343
1344   /* Mark our block as free */
1345   ret = bitmap_mark_free(img, block);
1346   if (ret) return ret;
1347
1348   return IMGTOOLERR_SUCCESS;
1349}
1350
1351
1352/* Returns the number of the first bit that is set in the array */
1353static int get_first_bit(UINT32 *array, int size)
1354{
1355   int i;
1356
1357   for (i = 0; i < size; i++)
1358   {
1359      if (array[i] != 0)
1360      {
1361         UINT32 v = array[i];
1362         int c;
1363
1364         /* get first bit that is set */
1365         for (c = 0; (v & 1) == 0; c++) v >>= 1;
1366
1367         /* return bit number */
1368         return i * 32 + c;
1369      }
1370   }
1371
1372   return -1;
1373}
1374
1375
1376static imgtoolerr_t walk_bitmap_ext_blocks(imgtool_image *img, int start, int *block)
1377{
1378   imgtoolerr_t ret;
1379   bitmap_ext_block bm_ext;
1380   int bit;
1381
1382   /* if we don't have a valid block, bail out */
1383   if (start == 0)
1384   {
1385      return IMGTOOLERR_NOSPACE;
1386   }
1387
1388   /* get the extended bitmap block */
1389   ret = read_bitmap_ext_block(img, start, &bm_ext);
1390   if (ret) return ret;
1391
1392   /* get the first bit that is set in the map */
1393   bit = get_first_bit(bm_ext.map, MSIZE);
1394
1395   /* if we found one, return */
1396   if (bit != -1)
1397   {
1398      *block += bit;
1399      return IMGTOOLERR_SUCCESS;
1400   }
1401
1402   /* increase on each call */
1403   *block += MSIZE * 32;
1404
1405   /* else continue walking the list */
1406   return walk_bitmap_ext_blocks(img, bm_ext.next, block);
1407}
1408
1409
1410/* Searches for a block marked as free
1411 * TODO: bm_ext support for HDs */
1412static imgtoolerr_t find_free_block(imgtool_image *img, int *block)
1413{
1414   imgtoolerr_t ret;
1415   root_block root;
1416   int i;
1417
1418   /* get root block */
1419   ret = read_root_block(img, &root);
1420   if (ret) return ret;
1421
1422   /* iterate bitmap pointers */
1423   for (i = 0; i < 25; i++)
1424   {
1425      bitmap_block bm;
1426
1427      /* get bitmap block pointed to */
1428      ret = read_bitmap_block(img, root.bm_pages[i], &bm);
1429      if (ret) return ret;
1430
1431      *block = i * 32 * MSIZE + get_first_bit(bm.map, MSIZE);
1432
1433      if (*block != -1)
1434      {
1435         *block += 2;
1436         return IMGTOOLERR_SUCCESS;
1437      }
1438   }
1439
1440   /* if we get here we haven't found a free block */
1441   return IMGTOOLERR_NOSPACE;
1442}
1443
1444
1445static imgtoolerr_t add_entry(imgtool_image *img, int parent, int block)
1446{
1447   imgtoolerr_t ret;
1448   UINT32 ht[TSIZE];
1449   char name[31];
1450   int hash;
1451
1452   ret = get_blockname(img, block, name);
1453   if (ret) return ret;
1454
1455   ret = get_hash_table(img, parent, ht);
1456   if (ret) return ret;
1457
1458   hash = hash_name(name, is_intl(img));
1459
1460   /* Check if there is already an entry with that name */
1461   if (ht[hash] != 0)
1462   {
1463      /* Save the old value to our hash chain */
1464      ret = set_hash_chain(img, block, ht[hash]);
1465      if (ret) return ret;
1466   }
1467
1468   /* Write our block number into the table */
1469   ht[hash] = block;
1470
1471   /* Write table back to disk */
1472   ret = set_hash_table(img, parent, ht);
1473   if (ret) return ret;
1474
1475   return IMGTOOLERR_SUCCESS;
1476}
1477
1478
1479/* Recursively create new directory entries */
1480static imgtoolerr_t makedir(imgtool_image *img, const char *path, int parent)
1481{
1482   imgtoolerr_t ret;
1483   dir_block dir;
1484   time_t now;
1485   int block;
1486
1487   if (!path)
1488   {
1489      return IMGTOOLERR_PARAMNEEDED;
1490   }
1491
1492   if (!path[0])
1493   {
1494      return IMGTOOLERR_SUCCESS;
1495   }
1496
1497   /* Get a free block */
1498   ret = find_free_block(img, &block);
1499   if (ret) return ret;
1500
1501   /* Initialize entry */
1502   memset(&dir, 0, sizeof(dir_block));
1503
1504   /* Copy data */
1505   time(&now);
1506   amiga_setup_time(now, &dir.date);
1507   dir.name_len = strlen(path);
1508   memcpy(dir.dirname, path, dir.name_len);
1509   dir.header_key = block;
1510   dir.parent = parent;
1511
1512   /* Write block */
1513   ret = write_dir_block(img, block, &dir);
1514   if (ret) return ret;
1515
1516   /* Fix checksum */
1517   ret = fix_block_chksum(img, block);
1518   if (ret) return ret;
1519
1520   /* Link the new entry in the parent */
1521   ret = add_entry(img, parent, block);
1522   if (ret) return ret;
1523
1524   /* Mark it as used */
1525   ret = bitmap_mark_used(img, block);
1526   if (ret) return ret;
1527
1528   /* Create the next entry */
1529   return makedir(img, path + strlen(path) + 1, block);
1530}
1531
1532
1533/* Recursively checks the path parts and creates directories for them */
1534static imgtoolerr_t checkdir(imgtool_image *img, const char *path, int parent)
1535{
1536   imgtoolerr_t ret;
1537   int block;
1538   char first_part[31];
1539
1540   memset(first_part, 0, sizeof(first_part));
1541   strcpy(first_part, path);
1542
1543   /* Directories all the way down, bail out */
1544   if (!path[0])
1545   {
1546      return IMGTOOLERR_CANNOTUSEPATH;
1547   }
1548
1549   /* Search for the entry */
1550   ret = find_entry(img, first_part, parent, &block);
1551
1552   switch (ret)
1553   {
1554   case IMGTOOLERR_PATHNOTFOUND:
1555
1556      /* There is no entry with this name yet, so we can just create them */
1557      return makedir(img, path, parent);
1558
1559   case IMGTOOLERR_SUCCESS:
1560
1561      if (get_block_type(img, block) == ST_USERDIR)
1562      {
1563         /* Go down one level */
1564         return checkdir(img, path + strlen(path) + 1, block);
1565      }
1566      else
1567      {
1568         /* There is an entry but it's not a directory, create it */
1569         return makedir(img, path, parent);
1570      }
1571
1572   default: return ret;
1573   }
1574}
1575
1576
1577/* Writes the file data from the specified block into the stream */
1578static imgtoolerr_t write_file_block_data(imgtool_image *img, int block, int size, imgtool_stream *destf)
1579{
1580   imgtoolerr_t ret;
1581   UINT8 buffer[BSIZE];
1582
1583   /* Check if we even need to write something */
1584   if (size == 0)
1585   {
1586      return IMGTOOLERR_SUCCESS;
1587   }
1588
1589   if (is_ffs(img))
1590   {
1591      /* Get block and read directly into buffer */
1592      ret = read_block(img, block, buffer);
1593      if (ret) return ret;
1594   }
1595   else
1596   {
1597      data_block db;
1598      UINT32 chksum;
1599
1600      ret = read_data_block(img, block, &db);
1601      if (ret) return ret;
1602
1603      /* Verify data checksum */
1604      ret = get_block_chksum(img, block, &chksum, FALSE);
1605      if (ret) return ret;
1606
1607      if (db.chksum != chksum)
1608      {
1609         return IMGTOOLERR_CORRUPTFILE;
1610      }
1611
1612      /* Copy data to buffer */
1613      memcpy(buffer, db.data, size);
1614   }
1615
1616   /* Write data to stream */
1617   if (stream_write (destf, buffer, size) != size)
1618   {
1619      return IMGTOOLERR_WRITEERROR;
1620   }
1621
1622   return IMGTOOLERR_SUCCESS;
1623}
1624
1625
1626static imgtoolerr_t walk_data_block_ptr(imgtool_image *img, UINT32 *ptr, int *filesize, imgtool_stream *destf, int write)
1627{
1628   int i, blocksize = is_ffs(img) ? BSIZE : BSIZE-24;
1629   imgtoolerr_t ret;
1630
1631   for (i = TSIZE-1; i >= 0; i--)
1632   {
1633      /* We write either blocksize bytes or whats remaining */
1634      int bytes_left = (*filesize >= blocksize) ? blocksize : *filesize;
1635
1636      if (write)
1637      {
1638         ret = write_file_block_data(img, ptr[i], bytes_left, destf);
1639         if (ret) return ret;
1640      }
1641      else
1642      {
1643         ret = bitmap_mark_free(img, ptr[i]);
1644         if (ret) return ret;
1645      }
1646
1647      *filesize -= bytes_left;
1648
1649      /* Check if we are finished early */
1650      if (*filesize == 0) break;
1651   }
1652
1653   return IMGTOOLERR_SUCCESS;
1654}
1655
1656
1657static imgtoolerr_t write_data_block_ptr(imgtool_image *img, UINT32 *ptr, int *filesize, imgtool_stream *destf)
1658{
1659   return walk_data_block_ptr(img, ptr, filesize, destf, TRUE);
1660}
1661
1662
1663/* Marks all blocks pointed to by the data block pointers as free */
1664static imgtoolerr_t clear_data_block_ptr(imgtool_image *img, UINT32 *ptr, int *filesize)
1665{
1666   return walk_data_block_ptr(img, ptr, filesize, NULL, FALSE);
1667}
1668
1669
1670static imgtoolerr_t walk_file_ext_data(imgtool_image *img, int block, int *filesize, imgtool_stream *destf, int write)
1671{
1672   file_ext_block file_ext;
1673   imgtoolerr_t ret;
1674
1675   /* Get file extension block */
1676   ret = read_file_ext_block(img, block, &file_ext);
1677   if (ret) return ret;
1678
1679   /* Write all data pointers in the table */
1680   ret = walk_data_block_ptr(img, file_ext.data_blocks, filesize, destf, write);
1681   if (ret) return ret;
1682
1683   /* Check if we are finished */
1684   if (*filesize != 0)
1685   {
1686      if (file_ext.extension == 0)
1687      {
1688         /* We are not finished, but there are no more extension blocks */
1689         return IMGTOOLERR_CORRUPTFILE;
1690      }
1691      else
1692      {
1693         /* Write the next file extension block */
1694         return walk_file_ext_data(img, file_ext.extension, filesize, destf, write);
1695      }
1696   }
1697
1698   /* Mark ourself as free if we not writing */
1699   if (!write)
1700   {
1701      ret = bitmap_mark_free(img, block);
1702      if (ret) return ret;
1703   }
1704
1705   return IMGTOOLERR_SUCCESS;
1706}
1707
1708
1709static imgtoolerr_t write_file_ext_data(imgtool_image *img, int block, int *filesize, imgtool_stream *destf)
1710{
1711   return walk_file_ext_data(img, block, filesize, destf, TRUE);
1712}
1713
1714
1715static imgtoolerr_t clear_file_ext_data(imgtool_image *img, int block, int *filesize)
1716{
1717   return walk_file_ext_data(img, block, filesize, NULL, FALSE);
1718}
1719
1720
1721/* Updates the disk alteration date stored in the root block */
1722static imgtoolerr_t update_disk_alteration_date(imgtool_image *img)
1723{
1724   imgtoolerr_t ret;
1725   root_block root;
1726   time_t now;
1727
1728   /* Get root block */
1729   ret = read_root_block(img, &root);
1730   if (ret) return ret;
1731
1732   /* Get current time */
1733   time(&now);
1734   amiga_setup_time(now, &root.v);
1735
1736   /* Write back new root block */
1737   ret = write_root_block(img, &root);
1738   if (ret) return ret;
1739
1740   /* And update its checksum */
1741   ret = fix_block_chksum(img, get_total_blocks(img)/2);
1742   if (ret) return ret;
1743
1744   return IMGTOOLERR_SUCCESS;
1745}
1746
1747
1748/*****************************************************************************
1749 Imgtool functions
1750*****************************************************************************/
1751
1752
1753static imgtoolerr_t amiga_image_open(imgtool_image *img, imgtool_stream *stream)
1754{
1755   amiga_floppy *f = (amiga_floppy *) imgtool_image_extra_bytes(img);
1756   UINT64 size = stream_size(stream);
1757
1758   f->stream = stream;
1759   f->sectors = size/BSIZE/80/2;
1760
1761   if (f->sectors != 11 && f->sectors != 22)
1762   {
1763      return IMGTOOLERR_CORRUPTIMAGE;
1764   }
1765
1766   return IMGTOOLERR_SUCCESS;
1767}
1768
1769
1770static void amiga_image_exit(imgtool_image *img)
1771{
1772}
1773
1774
1775static void amiga_image_info(imgtool_image *img, char *string, size_t len)
1776{
1777   imgtoolerr_t ret;
1778   root_block root;
1779
1780   ret = read_root_block(img, &root);
1781   if (ret) return;
1782
1783   memcpy(string, root.diskname, MIN(root.name_len, len));
1784   string[MIN(root.name_len, len)] = '\0';
1785}
1786
1787
1788static imgtoolerr_t amiga_image_read_sector(imgtool_image* img, UINT32 track, UINT32 head, UINT32 sector, void *buf, size_t len)
1789{
1790   amiga_floppy *f = (amiga_floppy *) imgtool_image_extra_bytes(img);
1791
1792   /* skip ahead to the area we want to read */
1793   stream_seek(f->stream, track * (head+1) * f->sectors * BSIZE + sector * BSIZE, SEEK_CUR);
1794
1795   if (stream_read(f->stream, buf, len) != len)
1796   {
1797      return IMGTOOLERR_READERROR;
1798   }
1799
1800   /* reset stream */
1801   stream_seek(f->stream, 0, 0);
1802
1803   return IMGTOOLERR_SUCCESS;
1804}
1805
1806
1807static imgtoolerr_t amiga_image_write_sector(imgtool_image* img, UINT32 track, UINT32 head, UINT32 sector, const void *buf, size_t len, int ddam)
1808{
1809   amiga_floppy *f = (amiga_floppy *) imgtool_image_extra_bytes(img);
1810
1811   /* skip ahead to the area we want to write */
1812   stream_seek(f->stream, track * (head+1) * f->sectors * BSIZE + sector * BSIZE, SEEK_CUR);
1813
1814   /* write data */
1815   if (stream_write(f->stream, buf, len) != len)
1816   {
1817      return IMGTOOLERR_WRITEERROR;
1818   }
1819
1820   /* reset stream */
1821   stream_seek(f->stream, 0, 0);
1822
1823   return IMGTOOLERR_SUCCESS;
1824}
1825
1826
1827static imgtoolerr_t amiga_image_get_sector_size(imgtool_image* img, UINT32 track, UINT32 head, UINT32 sector, UINT32 *sector_size)
1828{
1829   *sector_size = BSIZE;
1830   return IMGTOOLERR_SUCCESS;
1831}
1832
1833
1834static imgtoolerr_t amiga_image_beginenum(imgtool_directory *enumeration, const char *path)
1835{
1836   int blocks = get_total_blocks(imgtool_directory_image(enumeration));
1837   imgtoolerr_t ret;
1838   amiga_iterator *iter;
1839
1840   iter = (amiga_iterator *) imgtool_directory_extrabytes(enumeration);
1841   if (!iter) return IMGTOOLERR_OUTOFMEMORY;
1842
1843   iter->index = 1;
1844   iter->ht_index = 0;
1845   iter->eof = 0;
1846
1847   if (path[0])
1848   {
1849      /* search for the directory block, start with the root block */
1850      ret = find_entry(imgtool_directory_image(enumeration), path, blocks/2, &iter->block);
1851      if (ret) return ret;
1852   }
1853   else
1854   {
1855      /* we didn't get a path, use the root directory */
1856      iter->block = blocks / 2;
1857   }
1858
1859   return IMGTOOLERR_SUCCESS;
1860}
1861
1862
1863static imgtoolerr_t amiga_image_nextenum(imgtool_directory *enumeration, imgtool_dirent *ent)
1864{
1865   amiga_iterator *iter = (amiga_iterator *) imgtool_directory_extrabytes(enumeration);
1866   imgtoolerr_t ret;
1867   UINT32 ht[TSIZE];
1868   int block;
1869
1870   /* finished listing all entries? */
1871   if (iter->eof == 1 || iter->ht_index == TSIZE)
1872   {
1873      ent->eof = 1;
1874      return IMGTOOLERR_SUCCESS;
1875   }
1876
1877   /* get hash table */
1878   ret = get_hash_table(imgtool_directory_image(enumeration), iter->block, ht);
1879   if (ret) return ret;
1880
1881   /* skip empty hash table entries */
1882   while (ht[iter->ht_index] == 0)
1883   {
1884      iter->ht_index++;
1885      /* check if we are already at the end */
1886      if (iter->ht_index == TSIZE)
1887      {
1888         ent->eof = 1;
1889         return IMGTOOLERR_SUCCESS;
1890      }
1891   }
1892
1893   /* get block number */
1894   block = (iter->next_block == 0) ? ht[iter->ht_index] : iter->next_block;
1895
1896   switch (get_block_type(imgtool_directory_image(enumeration), block))
1897   {
1898   case ST_FILE:
1899   {
1900      file_block file;
1901
1902      /* get block */
1903      ret = read_file_block(imgtool_directory_image(enumeration), block, &file);
1904      if (ret) return ret;
1905
1906      /* fill directory entry */
1907      strncpyz(ent->filename, (char *)file.filename, file.name_len + 1);
1908      ent->filesize = file.byte_size;
1909      ent->lastmodified_time = amiga_crack_time(&file.date);
1910      amiga_decode_flags(file.protect, ent->attr);
1911      strncpyz(ent->comment, (char *)file.comment, file.comm_len + 1);
1912
1913      iter->next_block = file.hash_chain;
1914
1915      break;
1916   }
1917
1918   case ST_USERDIR:
1919   {
1920      dir_block dir;
1921
1922      /* get block */
1923      ret = read_dir_block(imgtool_directory_image(enumeration), block, &dir);
1924      if (ret) return ret;
1925
1926      /* fill directory entry */
1927      strncpyz(ent->filename, (char *)dir.dirname, dir.name_len + 1);
1928      ent->lastmodified_time = amiga_crack_time(&dir.date);
1929      amiga_decode_flags(dir.protect, ent->attr);
1930      strncpyz(ent->comment, (char *)dir.comment, dir.comm_len + 1);
1931      ent->directory = 1;
1932
1933      iter->next_block = dir.hash_chain;
1934
1935      break;
1936   }
1937
1938   case ST_SOFTLINK:
1939   {
1940      softlink_block sl;
1941
1942      /* get block */
1943      ret = read_softlink_block(imgtool_directory_image(enumeration), block, &sl);
1944      if (ret) return ret;
1945
1946      /* fill directory entry */
1947      strncpyz(ent->filename, (char *)sl.slname, sl.name_len + 1);
1948      ent->lastmodified_time = amiga_crack_time(&sl.date);
1949      amiga_decode_flags(sl.protect, ent->attr);
1950      strncpyz(ent->comment, (char *)sl.comment, sl.comm_len + 1);
1951      strcpy(ent->softlink, (char *)sl.symbolic_name);
1952
1953      iter->next_block = sl.hash_chain;
1954
1955      break;
1956   }
1957
1958   case ST_LINKDIR:
1959
1960      ent->directory = 1;
1961
1962   case ST_LINKFILE:
1963   {
1964      hardlink_block hl;
1965
1966      /* get block */
1967      ret = read_hardlink_block(imgtool_directory_image(enumeration), block, &hl);
1968      if (ret) return ret;
1969
1970      /* get filesize from linked file */
1971      if (!ent->directory)
1972      {
1973         file_block file;
1974         ret = read_file_block(imgtool_directory_image(enumeration), hl.real_entry, &file);
1975         if (ret) return ret;
1976         ent->filesize = file.byte_size;
1977      }
1978
1979      /* fill directory entry */
1980      strncpyz(ent->filename, (char *)hl.hlname, hl.name_len + 1);
1981      ent->lastmodified_time = amiga_crack_time(&hl.date);
1982      amiga_decode_flags(hl.protect, ent->attr);
1983      strncpyz(ent->comment, (char *)hl.comment, hl.comm_len + 1);
1984      ent->hardlink = 1;
1985
1986      iter->next_block = hl.hash_chain;
1987
1988      break;
1989   }
1990
1991   default:
1992      return IMGTOOLERR_UNIMPLEMENTED;
1993   }
1994
1995   /* if there are no linked entries, go to the next hash table entry */
1996   if (iter->next_block == 0)
1997   {
1998      iter->ht_index++;
1999   }
2000
2001   /* jump to next index */
2002   iter->index++;
2003
2004   return IMGTOOLERR_SUCCESS;
2005}
2006
2007
2008static void amiga_image_closeenum(imgtool_directory *enumeration)
2009{
2010   free(enumeration);
2011}
2012
2013
2014static imgtoolerr_t amiga_image_freespace(imgtool_partition *partition, UINT64 *size)
2015{
2016   imgtoolerr_t ret;
2017   imgtool_image *image = imgtool_partition_image(partition);
2018   const int data_size = is_ffs(image) ? BSIZE : BSIZE-24;
2019   root_block root;
2020   bitmap_block bm;
2021   int blocks, blocks_processed = 0, pages, i, c;
2022   UINT32 v;
2023
2024   /* initialize size */
2025   *size = 0;
2026
2027   /* get root block */
2028   ret = read_root_block(image, &root);
2029   if (ret) return ret;
2030
2031   /* get total number of blocks in the image */
2032   blocks = get_total_blocks(image);
2033
2034   /* subtract the two bootblock blocks (only for floppies!) */
2035   blocks -= 2;
2036
2037   /* iterate all bitmap pages */
2038   for (pages = 0; pages < 25; pages++)
2039   {
2040      ret = read_bitmap_block(image, root.bm_pages[pages], &bm);
2041      if (ret) return ret;
2042
2043      for (i = 0; i < MSIZE; i++)
2044      {
2045         v = bm.map[i];
2046
2047         /* clear half used value */
2048         if ((blocks_processed + 32) > blocks)
2049            v &= ~(~0 << (blocks - blocks_processed));
2050
2051         /* count bits */
2052         for (c = 0; v; c++)
2053            v &= v - 1;
2054
2055         *size += c * data_size;
2056
2057         blocks_processed += 32;
2058
2059         if (blocks_processed >= blocks)
2060            return IMGTOOLERR_SUCCESS;
2061      }
2062   }
2063
2064   return IMGTOOLERR_SUCCESS;
2065}
2066
2067
2068static imgtoolerr_t amiga_image_readfile(imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *destf)
2069{
2070   imgtool_image *img = imgtool_partition_image(partition);
2071   imgtoolerr_t ret;
2072   file_block file;
2073   int filesize, block;
2074
2075   /* Search for the block number */
2076   ret = find_entry(img, filename, get_total_blocks(img)/2, &block);
2077   if (ret == IMGTOOLERR_PATHNOTFOUND) return IMGTOOLERR_FILENOTFOUND;
2078   if (ret) return ret;  /* Other errors */
2079
2080   /* Phase 1: Follow data pointers */
2081   ret = read_file_block(img, block, &file);
2082   if (ret) return ret;
2083
2084   filesize = file.byte_size;
2085
2086   /* Write out file data pointed to by data block pointers */
2087   ret = write_data_block_ptr(img, file.data_blocks, &filesize, destf);
2088   if (ret) return ret;
2089
2090   /* Check if we are done already */
2091   if (filesize == 0) return IMGTOOLERR_SUCCESS;
2092
2093   /* Phase 2: Follow file extension blocks */
2094   ret = write_file_ext_data(img, file.extension, &filesize, destf);
2095   if (ret) return ret;
2096
2097   return IMGTOOLERR_SUCCESS;
2098}
2099
2100
2101/* When a file is deleted, only its File header block number is cleared from
2102 * the Directory block (or from the same-hash-value list) and the bitmap is
2103 * updated. File header block, Data blocks and File extension blocks are not
2104 * cleared, but the bitmap blocks are updated. */
2105static imgtoolerr_t amiga_image_deletefile(imgtool_partition *partition, const char *fname)
2106{
2107   imgtool_image *img = imgtool_partition_image(partition);
2108   imgtoolerr_t ret;
2109   int parent, block;
2110   char filename[31];
2111
2112   /* Initialize filename */
2113   memset(filename, 0, sizeof(filename));
2114
2115   /* Search for the block number */
2116   ret = find_entry(img, fname, get_total_blocks(img)/2, &block);
2117   if (ret == IMGTOOLERR_PATHNOTFOUND) return IMGTOOLERR_FILENOTFOUND;
2118   if (ret) return ret;
2119
2120   /* Get the parent block, where we need to clear the hash */
2121   switch (get_block_type(img, block))
2122   {
2123   case ST_FILE:
2124   {
2125      file_block file;
2126      int filesize;
2127
2128      ret = read_file_block(img, block, &file);
2129      if (ret) return ret;
2130
2131      filesize = file.byte_size;
2132      parent = file.parent;
2133      memcpy(filename, file.filename, file.name_len);
2134
2135      /* Clear all linked data sectors */
2136      ret = clear_data_block_ptr(img, file.data_blocks, &filesize);
2137      if (ret) return ret;
2138
2139      /* Clear extended file data sectors */
2140      if (filesize != 0)
2141      {
2142         ret = clear_file_ext_data(img, file.extension, &filesize);
2143         if (ret) return ret;
2144      }
2145
2146      break;
2147   }
2148
2149   case ST_LINKFILE:
2150   {
2151      softlink_block link;
2152
2153      ret = read_softlink_block(img, block, &link);
2154      if (ret) return ret;
2155
2156      parent = link.parent;
2157      memcpy(filename, link.slname, link.name_len);
2158
2159      break;
2160   }
2161
2162   default:
2163      return IMGTOOLERR_UNEXPECTED;
2164   }
2165
2166   /* Clear hash table entry */
2167   ret = clear_hash_table_entry(img, parent, filename);
2168   if (ret) return ret;
2169
2170   /* Update disk alteration date */
2171   ret = update_disk_alteration_date(img);
2172   if (ret) return ret;
2173
2174   return IMGTOOLERR_SUCCESS;
2175}
2176
2177
2178static imgtoolerr_t amiga_image_writefile(imgtool_partition *partition, const char *filename, const char *fork, imgtool_stream *sourcef, option_resolution *opts)
2179{
2180   return IMGTOOLERR_UNIMPLEMENTED;
2181}
2182
2183
2184static imgtoolerr_t amiga_image_create(imgtool_image *img, imgtool_stream *stream, option_resolution *opts)
2185{
2186   amiga_floppy *f = (amiga_floppy *) imgtool_image_extra_bytes(img);
2187   const char *dskname = option_resolution_lookup_string(opts, 'N');
2188   imgtoolerr_t ret;
2189   UINT8 buffer[BSIZE];
2190   root_block root;
2191   bitmap_block bm;
2192   time_t now;
2193   int blocks;
2194
2195   f->stream = stream;
2196
2197   switch (option_resolution_lookup_int(opts, 'S'))
2198   {
2199   case 0: f->sectors = 11; break;
2200   case 1: f->sectors = 22; break;
2201   default: return IMGTOOLERR_PARAMCORRUPT;
2202   }
2203
2204   /* initialize with zeros */
2205   memset(buffer, 0, BSIZE);
2206
2207   /* add DOS magic string and flags */
2208   buffer[0] = 'D';
2209   buffer[1] = 'O';
2210   buffer[2] = 'S';
2211   buffer[3] = 0;
2212
2213   /* File system */
2214   buffer[3] += (option_resolution_lookup_int(opts, 'F'));
2215
2216   /* File system mode */
2217   buffer[3] += (option_resolution_lookup_int(opts, 'M'));
2218
2219   /* write first bootblock sector */
2220   ret = write_block(img, 0, buffer);
2221   if (ret) return ret;
2222
2223   /* reset with zeros */
2224   memset(buffer, 0, BSIZE);
2225
2226   /* write second bootblock sector */
2227   ret = write_block(img, 1, buffer);
2228   if (ret) return ret;
2229
2230   /* rootblock */
2231   memset(&root, 0, sizeof(root_block));
2232
2233   blocks = get_total_blocks(img);
2234
2235   root.chksum      = 0;
2236   root.ht_size     = TSIZE;
2237   root.bm_flag     = -1;
2238   root.bm_pages[0] = blocks/2 + 1;  /* generally it's located here */
2239
2240   time(&now);
2241   amiga_setup_time(now, &root.r);
2242   amiga_setup_time(now, &root.v);
2243   amiga_setup_time(now, &root.c);
2244
2245   /* volume name */
2246   if (dskname)
2247   {
2248      root.name_len = strlen(dskname);
2249      memcpy(&root.diskname, dskname, root.name_len);
2250   }
2251   else
2252   {
2253      root.name_len = strlen("Empty");
2254      memcpy(&root.diskname, "Empty", root.name_len);
2255   }
2256
2257   /* write root block to disk */
2258   ret = write_root_block(img, &root);
2259   if (ret) return ret;
2260
2261   /* calculate block checksum */
2262   ret = fix_block_chksum(img, blocks/2);
2263   if (ret) return ret;
2264
2265   /* bitmap block */
2266   memset(&bm, 0xff, sizeof(bitmap_block));
2267
2268   /* write bitmap block to disk */
2269   ret = write_bitmap_block(img, root.bm_pages[0], &bm);
2270   if (ret) return ret;
2271
2272   /* set root and bitmap block as used */
2273   ret = bitmap_mark_used(img, blocks/2);
2274   if (ret) return ret;
2275
2276   ret = bitmap_mark_used(img, root.bm_pages[0]);
2277   if (ret) return ret;
2278
2279   /* write empty last block so that we don't get a truncated image */
2280   memset(buffer, 0, BSIZE);
2281
2282   ret = write_block(img, blocks - 1, buffer);
2283   if (ret) return ret;
2284
2285   return IMGTOOLERR_SUCCESS;
2286}
2287
2288
2289static imgtoolerr_t amiga_image_createdir(imgtool_partition *partition, const char *path)
2290{
2291   imgtool_image *img = imgtool_partition_image(partition);
2292   imgtoolerr_t ret;
2293
2294   /* Create directories */
2295   ret = checkdir(img, path, get_total_blocks(img)/2);
2296   if (ret) return ret;
2297
2298   /* Update disk alteration date */
2299   ret = update_disk_alteration_date(img);
2300   if (ret) return ret;
2301
2302   return IMGTOOLERR_SUCCESS;
2303}
2304
2305
2306static imgtoolerr_t amiga_image_getattrs(imgtool_partition *partition, const char *path, const UINT32 *attrs, imgtool_attribute *values)
2307{
2308   return IMGTOOLERR_UNIMPLEMENTED;
2309}
2310
2311
2312static imgtoolerr_t amiga_image_setattrs(imgtool_partition *partition, const char *path, const UINT32 *attrs, const imgtool_attribute *values)
2313{
2314   return IMGTOOLERR_UNIMPLEMENTED;
2315}
2316
2317
2318static imgtoolerr_t amiga_image_geticoninfo(imgtool_partition *partition, const char *path, imgtool_iconinfo *iconinfo)
2319{
2320   return IMGTOOLERR_UNIMPLEMENTED;
2321}
2322
2323
2324static imgtoolerr_t amiga_image_suggesttransfer(imgtool_partition *partition, const char *fname, imgtool_transfer_suggestion *suggestions, size_t suggestions_length)
2325{
2326   return IMGTOOLERR_UNIMPLEMENTED;
2327}
2328
2329
2330
2331/*****************************************************************************
2332 Create image options
2333*****************************************************************************/
2334
2335
2336OPTION_GUIDE_START(amiga_createimage_optionguide)
2337   OPTION_STRING( 'N', "name", "Volume name" )
2338   OPTION_ENUM_START( 'S', "density", "Density" )
2339      OPTION_ENUM( 0, "dd", "Double Density" )
2340      OPTION_ENUM( 1, "hd", "High Density" )
2341   OPTION_ENUM_END
2342   OPTION_ENUM_START( 'F', "filesystem", "File system" )
2343      OPTION_ENUM( 0, "ofs", "OldFileSystem" )
2344      OPTION_ENUM( 1, "ffs", "FastFileSystem" )
2345   OPTION_ENUM_END
2346   OPTION_ENUM_START( 'M', "mode", "File system options" )
2347      OPTION_ENUM( 0, "none",  "None" )
2348      OPTION_ENUM( 2, "intl", "International Mode" )
2349      OPTION_ENUM( 4, "dirc", "International Mode with Directory Cache" )
2350   OPTION_ENUM_END
2351OPTION_GUIDE_END
2352
2353
2354
2355/*****************************************************************************
2356 Imgtool module declaration
2357*****************************************************************************/
2358
2359
2360/* Amiga floppy disk attributes */
2361void amiga_floppy_get_info(const imgtool_class *imgclass, UINT32 state, union imgtoolinfo *info)
2362{
2363   switch(state)
2364   {
2365      /* --- the following bits of info are returned as 64-bit signed integers --- */
2366      case IMGTOOLINFO_INT_IMAGE_EXTRA_BYTES:          info->i = sizeof(amiga_floppy); break;
2367      case IMGTOOLINFO_INT_DIRECTORY_EXTRA_BYTES:      info->i = sizeof(amiga_iterator); break;
2368      case IMGTOOLINFO_INT_SUPPORTS_LASTMODIFIED_TIME: info->i = 1; break;
2369      case IMGTOOLINFO_INT_PATH_SEPARATOR:             info->i = '/'; break;
2370      case IMGTOOLINFO_INT_CHARSET:                    info->i = IMGTOOL_CHARSET_ISO_8859_1; break;
2371
2372      /* --- the following bits of info are returned as NULL-terminated strings --- */
2373      case IMGTOOLINFO_STR_NAME:                       strcpy(info->s = imgtool_temp_str(), "amiga_floppy"); break;
2374      case IMGTOOLINFO_STR_DESCRIPTION:                strcpy(info->s = imgtool_temp_str(), "Amiga floppy disk image (OFS/FFS format)"); break;
2375      case IMGTOOLINFO_STR_FILE_EXTENSIONS:            strcpy(info->s = imgtool_temp_str(), "adf"); break;
2376      case IMGTOOLINFO_STR_FILE:                       strcpy(info->s = imgtool_temp_str(), __FILE__); break;
2377      case IMGTOOLINFO_STR_EOLN:                       strcpy(info->s = imgtool_temp_str(), EOLN_LF); break;
2378      case IMGTOOLINFO_STR_CREATEIMAGE_OPTSPEC:        strcpy(info->s = imgtool_temp_str(), "S[0]-1;F[0]-1;M[0]/2/4"); break;
2379
2380      /* --- the following bits of info are returned as pointers to data or functions --- */
2381      case IMGTOOLINFO_PTR_OPEN:                       info->open = amiga_image_open; break;
2382      case IMGTOOLINFO_PTR_CLOSE:                      info->close = amiga_image_exit; break;
2383      case IMGTOOLINFO_PTR_READ_SECTOR:                info->read_sector = amiga_image_read_sector; break;
2384      case IMGTOOLINFO_PTR_WRITE_SECTOR:               info->write_sector = amiga_image_write_sector; break;
2385      case IMGTOOLINFO_PTR_GET_SECTOR_SIZE:            info->get_sector_size = amiga_image_get_sector_size; break;
2386      case IMGTOOLINFO_PTR_CREATE:                     info->create = amiga_image_create; break;
2387      case IMGTOOLINFO_PTR_INFO:                       info->info = amiga_image_info; break;
2388      case IMGTOOLINFO_PTR_BEGIN_ENUM:                 info->begin_enum = amiga_image_beginenum; break;
2389      case IMGTOOLINFO_PTR_NEXT_ENUM:                  info->next_enum = amiga_image_nextenum; break;
2390      case IMGTOOLINFO_PTR_CLOSE_ENUM:                 info->close_enum = amiga_image_closeenum; break;
2391      case IMGTOOLINFO_PTR_FREE_SPACE:                 info->free_space = amiga_image_freespace; break;
2392      case IMGTOOLINFO_PTR_READ_FILE:                  info->read_file = amiga_image_readfile; break;
2393      case IMGTOOLINFO_PTR_WRITE_FILE:                 info->write_file = amiga_image_writefile; break;
2394      case IMGTOOLINFO_PTR_DELETE_FILE:                info->delete_file = amiga_image_deletefile; break;
2395      case IMGTOOLINFO_PTR_CREATE_DIR:                 info->create_dir = amiga_image_createdir; break;
2396      case IMGTOOLINFO_PTR_GET_ATTRS:                  info->get_attrs = amiga_image_getattrs; break;
2397      case IMGTOOLINFO_PTR_SET_ATTRS:                  info->set_attrs = amiga_image_setattrs; break;
2398      case IMGTOOLINFO_PTR_GET_ICON_INFO:              info->get_iconinfo = amiga_image_geticoninfo; break;
2399      case IMGTOOLINFO_PTR_SUGGEST_TRANSFER:           info->suggest_transfer = amiga_image_suggesttransfer; break;
2400      case IMGTOOLINFO_PTR_CREATEIMAGE_OPTGUIDE:       info->createimage_optguide = amiga_createimage_optionguide; break;
2401   }
2402}
src/mess/formats/ami_dsk.c
r0r5845
1/*********************************************************************
2
3   formats/ami_dsk.c
4
5   Amiga disk images
6
7*********************************************************************/
8
9
10#include <string.h>
11
12#include "formats/ami_dsk.h"
13#include "formats/basicdsk.h"
14
15
16
17/*****************************************************************************
18 Amiga floppy core functions
19*****************************************************************************/
20
21
22static FLOPPY_IDENTIFY( amiga_dsk_identify )
23{
24   UINT64 size;
25   UINT8 header[4];
26   
27   *vote = 100;
28
29   /* get first 4 bytes */
30   floppy_image_read(floppy, &header, 0, sizeof(header));
31
32   /* first check the size of the image */
33   size = floppy_image_size(floppy);
34   if ((size != 901120) && (size != 1802240))
35      *vote = 0;
36
37   /* then check the first sector for the magic string */
38   if (memcmp(header, "DOS", 3))
39      *vote = 0;
40
41   /* then check the disk type */
42   if (header[3] > 5)
43      *vote = 0;
44
45   return FLOPPY_ERROR_SUCCESS;   
46}
47
48
49static FLOPPY_CONSTRUCT( amiga_dsk_construct )
50{
51   struct basicdsk_geometry geometry;
52
53   /* setup geometry with standard values */
54   memset(&geometry, 0, sizeof(geometry));
55   geometry.heads = 2;
56   geometry.tracks = 80;
57   geometry.first_sector_id = 0;
58   geometry.sector_length = 512;
59   
60   if (params)
61   {
62      /* create */
63      geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
64   }
65   else
66   {
67      /* open */
68      UINT64 size = floppy_image_size(floppy);
69      geometry.sectors = size/512/80/2;
70      if (geometry.sectors != 11 && geometry.sectors != 22)
71         return FLOPPY_ERROR_INVALIDIMAGE;     
72   }
73   
74   return basicdsk_construct(floppy, &geometry);
75}
76
77
78
79/*****************************************************************************
80 Amiga floppy options
81*****************************************************************************/
82
83
84FLOPPY_OPTIONS_START( amiga )
85   FLOPPY_OPTION(
86      ami_dsk,
87      "adf",
88      "Amiga floppy disk image",
89      amiga_dsk_identify,
90      amiga_dsk_construct,
91      HEADS([2])
92      TRACKS([80])
93      SECTORS([11]/22)
94   )
95FLOPPY_OPTIONS_END
src/mess/formats/ami_dsk.h
r0r5845
1/*********************************************************************
2
3   formats/ami_dsk.h
4
5   Amiga disk images
6
7*********************************************************************/
8
9#ifndef AMI_DSK_H_
10#define AMI_DSK_H_
11
12#include "formats/flopimg.h"
13
14FLOPPY_OPTIONS_EXTERN(amiga);
15
16#endif /*AMI_DSK_H_*/

Previous 15994 Revisions Next


© 1998-2018 The MESS Team