Theme Editor: Added extern C declarations to header files
[kugel-rb.git] / firmware / common / disk.c
blob5ef41bc296b192c88a0fc54cc1c373577a628c1e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include "storage.h"
23 #include "debug.h"
24 #include "fat.h"
25 #ifdef HAVE_HOTSWAP
26 #include "hotswap.h"
27 #include "dir.h" /* for release_dirs() */
28 #include "file.h" /* for release_files() */
29 #endif
30 #include "disk.h"
31 #include <string.h>
33 /* Partition table entry layout:
34 -----------------------
35 0: 0x80 - active
36 1: starting head
37 2: starting sector
38 3: starting cylinder
39 4: partition type
40 5: end head
41 6: end sector
42 7: end cylinder
43 8-11: starting sector (LBA)
44 12-15: nr of sectors in partition
47 #define BYTES2INT32(array,pos) \
48 ((long)array[pos] | ((long)array[pos+1] << 8 ) | \
49 ((long)array[pos+2] << 16 ) | ((long)array[pos+3] << 24 ))
51 static const unsigned char fat_partition_types[] = {
52 0x0b, 0x1b, /* FAT32 + hidden variant */
53 0x0c, 0x1c, /* FAT32 (LBA) + hidden variant */
54 #ifdef HAVE_FAT16SUPPORT
55 0x04, 0x14, /* FAT16 <= 32MB + hidden variant */
56 0x06, 0x16, /* FAT16 > 32MB + hidden variant */
57 0x0e, 0x1e, /* FAT16 (LBA) + hidden variant */
58 #endif
61 static struct partinfo part[NUM_DRIVES*4]; /* space for 4 partitions on 2 drives */
62 static int vol_drive[NUM_VOLUMES]; /* mounted to which drive (-1 if none) */
64 #ifdef MAX_LOG_SECTOR_SIZE
65 int disk_sector_multiplier = 1;
66 #endif
68 struct partinfo* disk_init(IF_MD_NONVOID(int drive))
70 int i;
71 unsigned char sector[SECTOR_SIZE];
72 #ifdef HAVE_MULTIDRIVE
73 /* For each drive, start at a different position, in order not to destroy
74 the first entry of drive 0.
75 That one is needed to calculate config sector position. */
76 struct partinfo* pinfo = &part[drive*4];
77 if ((size_t)drive >= sizeof(part)/sizeof(*part)/4)
78 return NULL; /* out of space in table */
79 #else
80 struct partinfo* pinfo = part;
81 const int drive = 0;
82 (void)drive;
83 #endif
85 storage_read_sectors(IF_MD2(drive,) 0,1, sector);
86 /* check that the boot sector is initialized */
87 if ( (sector[510] != 0x55) ||
88 (sector[511] != 0xaa)) {
89 DEBUGF("Bad boot sector signature\n");
90 return NULL;
93 /* parse partitions */
94 for ( i=0; i<4; i++ ) {
95 unsigned char* ptr = sector + 0x1be + 16*i;
96 pinfo[i].type = ptr[4];
97 pinfo[i].start = BYTES2INT32(ptr, 8);
98 pinfo[i].size = BYTES2INT32(ptr, 12);
100 DEBUGF("Part%d: Type %02x, start: %08lx size: %08lx\n",
101 i,pinfo[i].type,pinfo[i].start,pinfo[i].size);
103 /* extended? */
104 if ( pinfo[i].type == 5 ) {
105 /* not handled yet */
108 return pinfo;
111 struct partinfo* disk_partinfo(int partition)
113 return &part[partition];
116 int disk_mount_all(void)
118 int mounted=0;
119 int i;
121 #ifdef HAVE_HOTSWAP
122 card_enable_monitoring(false);
123 #endif
125 fat_init(); /* reset all mounted partitions */
126 for (i=0; i<NUM_VOLUMES; i++)
127 vol_drive[i] = -1; /* mark all as unassigned */
129 #ifndef HAVE_MULTIDRIVE
130 mounted = disk_mount(0);
131 #else
132 for(i=0;i<NUM_DRIVES;i++)
134 #ifdef HAVE_HOTSWAP
135 if (storage_present(i))
136 #endif
137 mounted += disk_mount(i);
139 #endif
141 #ifdef HAVE_HOTSWAP
142 card_enable_monitoring(true);
143 #endif
145 return mounted;
148 static int get_free_volume(void)
150 int i;
151 for (i=0; i<NUM_VOLUMES; i++)
153 if (vol_drive[i] == -1) /* unassigned? */
154 return i;
157 return -1; /* none found */
160 int disk_mount(int drive)
162 int mounted = 0; /* reset partition-on-drive flag */
163 int volume = get_free_volume();
164 struct partinfo* pinfo = disk_init(IF_MD(drive));
166 if (pinfo == NULL)
168 return 0;
170 #if defined(TOSHIBA_GIGABEAT_S)
171 int i = 1; /* For the Gigabeat S, we mount the second partition */
172 #else
173 int i = 0;
174 #endif
175 for (; volume != -1 && i<4 && mounted<NUM_VOLUMES_PER_DRIVE; i++)
177 if (memchr(fat_partition_types, pinfo[i].type,
178 sizeof(fat_partition_types)) == NULL)
179 continue; /* not an accepted partition type */
181 #ifdef MAX_LOG_SECTOR_SIZE
182 int j;
184 for (j = 1; j <= (MAX_LOG_SECTOR_SIZE/SECTOR_SIZE); j <<= 1)
186 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) pinfo[i].start * j))
188 pinfo[i].start *= j;
189 pinfo[i].size *= j;
190 mounted++;
191 vol_drive[volume] = drive; /* remember the drive for this volume */
192 volume = get_free_volume(); /* prepare next entry */
193 if (drive == 0)
194 disk_sector_multiplier = j;
195 break;
198 #else
199 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) pinfo[i].start))
201 mounted++;
202 vol_drive[volume] = drive; /* remember the drive for this volume */
203 volume = get_free_volume(); /* prepare next entry */
205 #endif
208 if (mounted == 0 && volume != -1) /* none of the 4 entries worked? */
209 { /* try "superfloppy" mode */
210 DEBUGF("No partition found, trying to mount sector 0.\n");
211 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) 0))
213 mounted = 1;
214 vol_drive[volume] = drive; /* remember the drive for this volume */
217 return mounted;
220 #ifdef HAVE_HOTSWAP
221 int disk_unmount(int drive)
223 int unmounted = 0;
224 int i;
225 for (i=0; i<NUM_VOLUMES; i++)
227 if (vol_drive[i] == drive)
228 { /* force releasing resources */
229 vol_drive[i] = -1; /* mark unused */
230 unmounted++;
231 release_files(i);
232 release_dirs(i);
233 fat_unmount(i, false);
237 return unmounted;
239 #endif /* #ifdef HAVE_HOTSWAP */