Add Łukasz Stelmach to CREDITS for FS#11785
[maemo-rb.git] / firmware / common / disk.c
bloba23d51dbb85510474920207c725f12111b7d202e
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 #include "dir.h" /* for release_dirs() */
27 #include "file.h" /* for release_files() */
28 #include "disk.h"
29 #include <string.h>
31 /* Partition table entry layout:
32 -----------------------
33 0: 0x80 - active
34 1: starting head
35 2: starting sector
36 3: starting cylinder
37 4: partition type
38 5: end head
39 6: end sector
40 7: end cylinder
41 8-11: starting sector (LBA)
42 12-15: nr of sectors in partition
45 #define BYTES2INT32(array,pos) \
46 ((long)array[pos] | ((long)array[pos+1] << 8 ) | \
47 ((long)array[pos+2] << 16 ) | ((long)array[pos+3] << 24 ))
49 static const unsigned char fat_partition_types[] = {
50 0x0b, 0x1b, /* FAT32 + hidden variant */
51 0x0c, 0x1c, /* FAT32 (LBA) + hidden variant */
52 #ifdef HAVE_FAT16SUPPORT
53 0x04, 0x14, /* FAT16 <= 32MB + hidden variant */
54 0x06, 0x16, /* FAT16 > 32MB + hidden variant */
55 0x0e, 0x1e, /* FAT16 (LBA) + hidden variant */
56 #endif
59 static struct partinfo part[NUM_DRIVES*4]; /* space for 4 partitions on 2 drives */
60 static int vol_drive[NUM_VOLUMES]; /* mounted to which drive (-1 if none) */
61 static struct mutex disk_mutex;
63 #ifdef MAX_LOG_SECTOR_SIZE
64 int disk_sector_multiplier = 1;
65 #endif
67 struct partinfo* disk_init(IF_MD_NONVOID(int drive))
69 int i;
70 unsigned char sector[SECTOR_SIZE];
71 #ifdef HAVE_MULTIDRIVE
72 /* For each drive, start at a different position, in order not to destroy
73 the first entry of drive 0.
74 That one is needed to calculate config sector position. */
75 struct partinfo* pinfo = &part[drive*4];
76 if ((size_t)drive >= sizeof(part)/sizeof(*part)/4)
77 return NULL; /* out of space in table */
78 #else
79 struct partinfo* pinfo = part;
80 const int drive = 0;
81 (void)drive;
82 #endif
84 storage_read_sectors(IF_MD2(drive,) 0,1, sector);
85 /* check that the boot sector is initialized */
86 if ( (sector[510] != 0x55) ||
87 (sector[511] != 0xaa)) {
88 DEBUGF("Bad boot sector signature\n");
89 return NULL;
92 /* parse partitions */
93 for ( i=0; i<4; i++ ) {
94 unsigned char* ptr = sector + 0x1be + 16*i;
95 pinfo[i].type = ptr[4];
96 pinfo[i].start = BYTES2INT32(ptr, 8);
97 pinfo[i].size = BYTES2INT32(ptr, 12);
99 DEBUGF("Part%d: Type %02x, start: %08lx size: %08lx\n",
100 i,pinfo[i].type,pinfo[i].start,pinfo[i].size);
102 /* extended? */
103 if ( pinfo[i].type == 5 ) {
104 /* not handled yet */
107 return pinfo;
110 struct partinfo* disk_partinfo(int partition)
112 return &part[partition];
115 void disk_init_subsystem(void)
117 mutex_init(&disk_mutex);
120 int disk_mount_all(void)
122 int mounted=0;
123 int i;
125 #ifdef HAVE_HOTSWAP
126 mutex_lock(&disk_mutex);
127 #endif
129 fat_init(); /* reset all mounted partitions */
130 for (i=0; i<NUM_VOLUMES; i++)
131 vol_drive[i] = -1; /* mark all as unassigned */
133 #ifndef HAVE_MULTIDRIVE
134 mounted = disk_mount(0);
135 #else
136 for(i=0;i<NUM_DRIVES;i++)
138 #ifdef HAVE_HOTSWAP
139 if (storage_present(i))
140 #endif
141 mounted += disk_mount(i);
143 #endif
145 #ifdef HAVE_HOTSWAP
146 mutex_unlock(&disk_mutex);
147 #endif
148 return mounted;
151 static int get_free_volume(void)
153 int i;
154 for (i=0; i<NUM_VOLUMES; i++)
156 if (vol_drive[i] == -1) /* unassigned? */
157 return i;
160 return -1; /* none found */
163 int disk_mount(int drive)
165 int mounted = 0; /* reset partition-on-drive flag */
166 int volume;
167 struct partinfo* pinfo;
169 #ifdef HAVE_HOTSWAP
170 mutex_lock(&disk_mutex);
171 #endif
173 volume = get_free_volume();
174 pinfo = disk_init(IF_MD(drive));
176 if (pinfo == NULL)
178 #ifdef HAVE_HOTSWAP
179 mutex_unlock(&disk_mutex);
180 #endif
181 return 0;
183 #if defined(TOSHIBA_GIGABEAT_S)
184 int i = 1; /* For the Gigabeat S, we mount the second partition */
185 #else
186 int i = 0;
187 #endif
188 for (; volume != -1 && i<4 && mounted<NUM_VOLUMES_PER_DRIVE; i++)
190 if (memchr(fat_partition_types, pinfo[i].type,
191 sizeof(fat_partition_types)) == NULL)
192 continue; /* not an accepted partition type */
194 #ifdef MAX_LOG_SECTOR_SIZE
195 int j;
197 for (j = 1; j <= (MAX_LOG_SECTOR_SIZE/SECTOR_SIZE); j <<= 1)
199 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) pinfo[i].start * j))
201 pinfo[i].start *= j;
202 pinfo[i].size *= j;
203 mounted++;
204 vol_drive[volume] = drive; /* remember the drive for this volume */
205 volume = get_free_volume(); /* prepare next entry */
206 if (drive == 0)
207 disk_sector_multiplier = j;
208 break;
211 #else
212 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) pinfo[i].start))
214 mounted++;
215 vol_drive[volume] = drive; /* remember the drive for this volume */
216 volume = get_free_volume(); /* prepare next entry */
218 #endif
221 if (mounted == 0 && volume != -1) /* none of the 4 entries worked? */
222 { /* try "superfloppy" mode */
223 DEBUGF("No partition found, trying to mount sector 0.\n");
224 if (!fat_mount(IF_MV2(volume,) IF_MD2(drive,) 0))
226 #ifdef MAX_LOG_SECTOR_SIZE
227 disk_sector_multiplier = fat_get_bytes_per_sector(IF_MV(volume))/SECTOR_SIZE;
228 #endif
229 mounted = 1;
230 vol_drive[volume] = drive; /* remember the drive for this volume */
233 #ifdef HAVE_HOTSWAP
234 mutex_unlock(&disk_mutex);
235 #endif
236 return mounted;
239 int disk_unmount(int drive)
241 int unmounted = 0;
242 int i;
243 #ifdef HAVE_HOTSWAP
244 mutex_lock(&disk_mutex);
245 #endif
246 for (i=0; i<NUM_VOLUMES; i++)
248 if (vol_drive[i] == drive)
249 { /* force releasing resources */
250 vol_drive[i] = -1; /* mark unused */
251 unmounted++;
252 release_files(i);
253 release_dirs(i);
254 fat_unmount(i, false);
257 #ifdef HAVE_HOTSWAP
258 mutex_unlock(&disk_mutex);
259 #endif
261 return unmounted;
264 int disk_unmount_all(void)
266 #ifndef HAVE_MULTIDRIVE
267 return disk_unmount(0);
268 #else /* HAVE_MULTIDRIVE */
269 int unmounted = 0;
270 int i;
271 for (i = 0; i < NUM_DRIVES; i++)
273 #ifdef HAVE_HOTSWAP
274 if (storage_present(i))
275 #endif
276 unmounted += disk_unmount(i);
279 return unmounted;
280 #endif /* HAVE_MULTIDRIVE */