Remove card_enable_monitoring() and use a mutex instead. The card_enable_monitoring...
[kugel-rb.git] / firmware / common / disk.c
blobbc0ad793d214b9d4a12c1248b287d2131cff3424
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 "kernel.h"
23 #include "storage.h"
24 #include "debug.h"
25 #include "fat.h"
26 #ifdef HAVE_HOTSWAP
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) */
63 static struct mutex disk_mutex;
65 #ifdef MAX_LOG_SECTOR_SIZE
66 int disk_sector_multiplier = 1;
67 #endif
69 static struct partinfo* disk_init(IF_MD_NONVOID(int drive))
71 int i;
72 unsigned char sector[SECTOR_SIZE];
73 #ifdef HAVE_MULTIDRIVE
74 /* For each drive, start at a different position, in order not to destroy
75 the first entry of drive 0.
76 That one is needed to calculate config sector position. */
77 struct partinfo* pinfo = &part[drive*4];
78 if ((size_t)drive >= sizeof(part)/sizeof(*part)/4)
79 return NULL; /* out of space in table */
80 #else
81 struct partinfo* pinfo = part;
82 const int drive = 0;
83 (void)drive;
84 #endif
86 storage_read_sectors(IF_MD2(drive,) 0,1, sector);
87 /* check that the boot sector is initialized */
88 if ( (sector[510] != 0x55) ||
89 (sector[511] != 0xaa)) {
90 DEBUGF("Bad boot sector signature\n");
91 return NULL;
94 /* parse partitions */
95 for ( i=0; i<4; i++ ) {
96 unsigned char* ptr = sector + 0x1be + 16*i;
97 pinfo[i].type = ptr[4];
98 pinfo[i].start = BYTES2INT32(ptr, 8);
99 pinfo[i].size = BYTES2INT32(ptr, 12);
101 DEBUGF("Part%d: Type %02x, start: %08lx size: %08lx\n",
102 i,pinfo[i].type,pinfo[i].start,pinfo[i].size);
104 /* extended? */
105 if ( pinfo[i].type == 5 ) {
106 /* not handled yet */
109 return pinfo;
112 struct partinfo* disk_partinfo(int partition)
114 return &part[partition];
117 void disk_init_subsystem(void)
119 mutex_init(&disk_mutex);
122 int disk_mount_all(void)
124 int mounted=0;
125 int i;
127 #ifdef HAVE_HOTSWAP
128 mutex_lock(&disk_mutex);
129 #endif
131 fat_init(); /* reset all mounted partitions */
132 for (i=0; i<NUM_VOLUMES; i++)
133 vol_drive[i] = -1; /* mark all as unassigned */
135 #ifndef HAVE_MULTIDRIVE
136 mounted = disk_mount(0);
137 #else
138 for(i=0;i<NUM_DRIVES;i++)
140 #ifdef HAVE_HOTSWAP
141 if (storage_present(i))
142 #endif
143 mounted += disk_mount(i);
145 #endif
147 #ifdef HAVE_HOTSWAP
148 mutex_unlock(&disk_mutex);
149 #endif
150 return mounted;
153 static int get_free_volume(void)
155 int i;
156 for (i=0; i<NUM_VOLUMES; i++)
158 if (vol_drive[i] == -1) /* unassigned? */
159 return i;
162 return -1; /* none found */
165 int disk_mount(int drive)
167 int mounted = 0; /* reset partition-on-drive flag */
168 int volume;
169 struct partinfo* pinfo;
171 #ifdef HAVE_HOTSWAP
172 mutex_lock(&disk_mutex);
173 #endif
175 volume = get_free_volume();
176 pinfo = disk_init(IF_MD(drive));
178 if (pinfo == NULL)
180 #ifdef HAVE_HOTSWAP
181 mutex_unlock(&disk_mutex);
182 #endif
183 return 0;
185 #if defined(TOSHIBA_GIGABEAT_S)
186 int i = 1; /* For the Gigabeat S, we mount the second partition */
187 #else
188 int i = 0;
189 #endif
190 for (; volume != -1 && i<4 && mounted<NUM_VOLUMES_PER_DRIVE; i++)
192 if (memchr(fat_partition_types, pinfo[i].type,
193 sizeof(fat_partition_types)) == NULL)
194 continue; /* not an accepted partition type */
196 #ifdef MAX_LOG_SECTOR_SIZE
197 int j;
199 for (j = 1; j <= (MAX_LOG_SECTOR_SIZE/SECTOR_SIZE); j <<= 1)
201 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) pinfo[i].start * j))
203 pinfo[i].start *= j;
204 pinfo[i].size *= j;
205 mounted++;
206 vol_drive[volume] = drive; /* remember the drive for this volume */
207 volume = get_free_volume(); /* prepare next entry */
208 if (drive == 0)
209 disk_sector_multiplier = j;
210 break;
213 #else
214 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) pinfo[i].start))
216 mounted++;
217 vol_drive[volume] = drive; /* remember the drive for this volume */
218 volume = get_free_volume(); /* prepare next entry */
220 #endif
223 if (mounted == 0 && volume != -1) /* none of the 4 entries worked? */
224 { /* try "superfloppy" mode */
225 DEBUGF("No partition found, trying to mount sector 0.\n");
226 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) 0))
228 mounted = 1;
229 vol_drive[volume] = drive; /* remember the drive for this volume */
232 #ifdef HAVE_HOTSWAP
233 mutex_unlock(&disk_mutex);
234 #endif
235 return mounted;
238 #ifdef HAVE_HOTSWAP
239 int disk_unmount(int drive)
241 int unmounted = 0;
242 int i;
243 mutex_lock(&disk_mutex);
244 for (i=0; i<NUM_VOLUMES; i++)
246 if (vol_drive[i] == drive)
247 { /* force releasing resources */
248 vol_drive[i] = -1; /* mark unused */
249 unmounted++;
250 release_files(i);
251 release_dirs(i);
252 fat_unmount(i, false);
255 mutex_unlock(&disk_mutex);
257 return unmounted;
259 #endif /* #ifdef HAVE_HOTSWAP */