~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Source/Core/Common/Src/SDCardUtil.cpp

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mksdcard.c
 
2
**
 
3
** Copyright 2007, The Android Open Source Project
 
4
**
 
5
** Redistribution and use in source and binary forms, with or without
 
6
** modification, are permitted provided that the following conditions are met:
 
7
**     * Redistributions of source code must retain the above copyright
 
8
**       notice, this list of conditions and the following disclaimer.
 
9
**     * Redistributions in binary form must reproduce the above copyright
 
10
**       notice, this list of conditions and the following disclaimer in the
 
11
**       documentation and/or other materials provided with the distribution.
 
12
**     * Neither the name of Google Inc. nor the names of its contributors may
 
13
**       be used to endorse or promote products derived from this software
 
14
**       without specific prior written permission.
 
15
**
 
16
** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
 
17
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
18
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 
19
** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
20
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
22
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
23
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
24
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
25
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
*/
 
27
 
 
28
// A simple and portable piece of code used to generate a blank FAT32 image file.
 
29
// Modified for Dolphin.
 
30
 
 
31
#include "SDCardUtil.h"
 
32
#include "FileUtil.h"
 
33
 
 
34
#include <time.h>
 
35
#include <stdio.h>
 
36
#include <stdlib.h>
 
37
#include <string.h>
 
38
#include <errno.h>
 
39
 
 
40
#ifndef _WIN32
 
41
#include <unistd.h> // for unlink()
 
42
#endif
 
43
 
 
44
/* Believe me, you *don't* want to change these constants !! */
 
45
#define BYTES_PER_SECTOR        512
 
46
#define RESERVED_SECTORS        32
 
47
#define BACKUP_BOOT_SECTOR      6
 
48
#define NUM_FATS                        2
 
49
 
 
50
#define BYTE_(p,i)      (((u8*)(p))[(i)])
 
51
 
 
52
#define POKEB(p,v)      BYTE_(p,0) = (u8)(v)
 
53
#define POKES(p,v)      ( BYTE_(p,0) = (u8)(v), BYTE_(p,1) = (u8)((v) >> 8) )
 
54
#define POKEW(p,v)      ( BYTE_(p,0) = (u8)(v), BYTE_(p,1) = (u8)((v) >> 8), BYTE_(p,2) = (u8)((v) >> 16), BYTE_(p,3) = (u8)((v) >> 24) )
 
55
 
 
56
static u8 s_boot_sector         [ BYTES_PER_SECTOR ];   /* Boot sector */
 
57
static u8 s_fsinfo_sector       [ BYTES_PER_SECTOR ];   /* FS Info sector */
 
58
static u8 s_fat_head            [ BYTES_PER_SECTOR ];   /* First FAT sector */
 
59
 
 
60
/* This is the date and time when creating the disk */
 
61
static unsigned int get_serial_id()
 
62
{
 
63
        u16                     lo, hi;
 
64
        time_t          now = time(nullptr);
 
65
        struct tm       tm  = gmtime( &now )[0];
 
66
 
 
67
        lo = (u16)(tm.tm_mday + ((tm.tm_mon+1) << 8) + (tm.tm_sec << 8));
 
68
        hi = (u16)(tm.tm_min + (tm.tm_hour << 8) + (tm.tm_year + 1900));
 
69
 
 
70
        return lo + (hi << 16);
 
71
}
 
72
 
 
73
static unsigned int get_sectors_per_cluster(u64 disk_size)
 
74
{
 
75
        u64 disk_MB = disk_size/(1024*1024);
 
76
 
 
77
        if (disk_MB < 260)
 
78
                return 1;
 
79
 
 
80
        if (disk_MB < 8192)
 
81
                return 4;
 
82
 
 
83
        if (disk_MB < 16384)
 
84
                return 8;
 
85
 
 
86
        if (disk_MB < 32768)
 
87
                return 16;
 
88
 
 
89
        return 32;
 
90
}
 
91
 
 
92
static unsigned int get_sectors_per_fat(u64 disk_size, u32 sectors_per_cluster)
 
93
{
 
94
        u64 divider;
 
95
 
 
96
        /* Weird computation from MS - see fatgen103.doc for details */
 
97
        disk_size -= RESERVED_SECTORS * BYTES_PER_SECTOR;       /* Don't count 32 reserved sectors */
 
98
        disk_size /= BYTES_PER_SECTOR;  /* Disk size in sectors */
 
99
        divider = ((256 * sectors_per_cluster) + NUM_FATS) / 2;
 
100
 
 
101
        return (u32)( (disk_size + (divider-1)) / divider );
 
102
}
 
103
 
 
104
static void boot_sector_init(u8* boot, u8* info, u64 disk_size, const char* label)
 
105
{
 
106
        u32 sectors_per_cluster = get_sectors_per_cluster(disk_size);
 
107
        u32 sectors_per_fat             = get_sectors_per_fat(disk_size, sectors_per_cluster);
 
108
        u32 sectors_per_disk    = (u32)(disk_size / BYTES_PER_SECTOR);
 
109
        u32 serial_id                   = get_serial_id();
 
110
        u32 free_count;
 
111
 
 
112
        if (label == nullptr)
 
113
                label = "DOLPHINSD";
 
114
 
 
115
        POKEB(boot, 0xeb);
 
116
        POKEB(boot+1, 0x5a);
 
117
        POKEB(boot+2, 0x90);
 
118
        strcpy( (char*)boot + 3, "MSWIN4.1" );
 
119
        POKES( boot + 0x0b, BYTES_PER_SECTOR );         /* Sector size */
 
120
        POKEB( boot + 0xd, sectors_per_cluster );       /* Sectors per cluster */
 
121
        POKES( boot + 0xe, RESERVED_SECTORS );          /* Reserved sectors before first FAT */
 
122
        POKEB( boot + 0x10, NUM_FATS );                         /* Number of FATs */
 
123
        POKES( boot + 0x11, 0 );                                        /* Max root directory entries for FAT12/FAT16, 0 for FAT32 */
 
124
        POKES( boot + 0x13, 0 );                                        /* Total sectors, 0 to use 32-bit value at offset 0x20 */
 
125
        POKEB( boot + 0x15, 0xF8 );                                     /* Media descriptor, 0xF8 == hard disk */
 
126
        POKES( boot + 0x16, 0 );                                        /* Sectors per FAT for FAT12/16, 0 for FAT32 */
 
127
        POKES( boot + 0x18, 9 );                                        /* Sectors per track (whatever) */
 
128
        POKES( boot + 0x1a, 2 );                                        /* Number of heads (whatever) */
 
129
        POKEW( boot + 0x1c, 0 );                                        /* Hidden sectors */
 
130
        POKEW( boot + 0x20, sectors_per_disk );         /* Total sectors */
 
131
 
 
132
        /* Extension */
 
133
        POKEW( boot + 0x24, sectors_per_fat );          /* Sectors per FAT */
 
134
        POKES( boot + 0x28, 0 );                                        /* FAT flags */
 
135
        POKES( boot + 0x2a, 0 );                                        /* Version */
 
136
        POKEW( boot + 0x2c, 2 );                                        /* Cluster number of root directory start */
 
137
        POKES( boot + 0x30, 1 );                                        /* Sector number of FS information sector */
 
138
        POKES( boot + 0x32, BACKUP_BOOT_SECTOR );       /* Sector number of a copy of this boot sector */
 
139
        POKEB( boot + 0x40, 0x80 );                                     /* Physical drive number */
 
140
        POKEB( boot + 0x42, 0x29 );                                     /* Extended boot signature ?? */
 
141
        POKEW( boot + 0x43, serial_id );                        /* Serial ID */
 
142
        strncpy( (char*)boot + 0x47, label, 11 );       /* Volume Label */
 
143
        memcpy( boot + 0x52, "FAT32   ", 8 );           /* FAT system type, padded with 0x20 */
 
144
 
 
145
        POKEB( boot + BYTES_PER_SECTOR-2, 0x55 );       /* Boot sector signature */
 
146
        POKEB( boot + BYTES_PER_SECTOR-1, 0xAA );
 
147
 
 
148
        /* FSInfo sector */
 
149
        free_count = sectors_per_disk - 32 - 2*sectors_per_fat;
 
150
 
 
151
        POKEW( info + 0,   0x41615252 );
 
152
        POKEW( info + 484, 0x61417272 );
 
153
        POKEW( info + 488, free_count );        /* Number of free clusters */
 
154
        POKEW( info + 492, 3 );                         /* Next free clusters, 0-1 reserved, 2 is used for the root dir */
 
155
        POKEW( info + 508, 0xAA550000 );
 
156
}
 
157
 
 
158
static void fat_init(u8* fat)
 
159
{
 
160
        POKEW( fat,     0x0ffffff8 );   /* Reserve cluster 1, media id in low byte */
 
161
        POKEW( fat + 4, 0x0fffffff );   /* Reserve cluster 2 */
 
162
        POKEW( fat + 8, 0x0fffffff );   /* End of cluster chain for root dir */
 
163
}
 
164
 
 
165
 
 
166
static unsigned int write_sector(FILE* file, u8* sector)
 
167
{
 
168
        return fwrite(sector, 1, 512, file) != 512;
 
169
}
 
170
 
 
171
static unsigned int write_empty(FILE* file, u64 count)
 
172
{
 
173
        static u8 empty[64*1024];
 
174
 
 
175
        count *= 512;
 
176
        while (count > 0)
 
177
        {
 
178
                u64 len = sizeof(empty);
 
179
                if (len > count)
 
180
                        len = count;
 
181
 
 
182
                if ( fwrite(empty, 1, (size_t)len, file) != (size_t)len )
 
183
                        return 1;
 
184
 
 
185
                count -= len;
 
186
        }
 
187
        return 0;
 
188
}
 
189
 
 
190
bool SDCardCreate(u64 disk_size /*in MB*/, const char* filename)
 
191
{
 
192
        u32 sectors_per_fat;
 
193
        u32 sectors_per_disk;
 
194
 
 
195
        // Convert MB to bytes
 
196
        disk_size *= 1024 * 1024;
 
197
 
 
198
        if (disk_size < 0x800000 || disk_size > 0x800000000ULL) {
 
199
                ERROR_LOG(COMMON, "Trying to create SD Card image of size %lliMB is out of range (8MB-32GB)", disk_size/(1024*1024));
 
200
                return false;
 
201
        }
 
202
 
 
203
        // Pretty unlikely to overflow.
 
204
        sectors_per_disk = (u32)(disk_size / 512);
 
205
        sectors_per_fat  = get_sectors_per_fat(disk_size, get_sectors_per_cluster(disk_size));
 
206
 
 
207
        boot_sector_init(s_boot_sector, s_fsinfo_sector, disk_size, nullptr);
 
208
        fat_init(s_fat_head);
 
209
 
 
210
        File::IOFile file(filename, "wb");
 
211
        FILE* const f = file.GetHandle();
 
212
        if (!f)
 
213
        {
 
214
                ERROR_LOG(COMMON, "Could not create file '%s', aborting...\n", filename);
 
215
                return false;
 
216
        }
 
217
 
 
218
        /* Here's the layout:
 
219
        *
 
220
        *  boot_sector
 
221
        *  fsinfo_sector
 
222
        *  empty
 
223
        *  backup boot sector
 
224
        *  backup fsinfo sector
 
225
        *  RESERVED_SECTORS - 4 empty sectors (if backup sectors), or RESERVED_SECTORS - 2 (if no backup)
 
226
        *  first fat
 
227
        *  second fat
 
228
        *  zero sectors
 
229
        */
 
230
 
 
231
        if (write_sector(f, s_boot_sector))             goto FailWrite;
 
232
        if (write_sector(f, s_fsinfo_sector))   goto FailWrite;
 
233
        if (BACKUP_BOOT_SECTOR > 0)
 
234
        {
 
235
                if (write_empty(f, BACKUP_BOOT_SECTOR - 2)) goto FailWrite;
 
236
                if (write_sector(f, s_boot_sector))             goto FailWrite;
 
237
                if (write_sector(f, s_fsinfo_sector))   goto FailWrite;
 
238
                if (write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) goto FailWrite;
 
239
        }
 
240
        else
 
241
                if (write_empty(f, RESERVED_SECTORS - 2)) goto FailWrite;
 
242
 
 
243
        if (write_sector(f, s_fat_head))                goto FailWrite;
 
244
        if (write_empty(f, sectors_per_fat-1))  goto FailWrite;
 
245
 
 
246
        if (write_sector(f, s_fat_head))                goto FailWrite;
 
247
        if (write_empty(f, sectors_per_fat-1))  goto FailWrite;
 
248
 
 
249
        if (write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2*sectors_per_fat)) goto FailWrite;
 
250
 
 
251
        return true;
 
252
 
 
253
FailWrite:
 
254
        ERROR_LOG(COMMON, "Could not write to '%s', aborting...\n", filename);
 
255
        if (unlink(filename) < 0)
 
256
                ERROR_LOG(COMMON, "unlink(%s) failed\n%s", filename, GetLastErrorMsg());
 
257
        return false;
 
258
}