1/*
2 * Copyright (c) 1998-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * This header contains the IOApplePartitionScheme class definition.
26 */
27
28#ifndef _IOAPPLEPARTITIONSCHEME_H
29#define _IOAPPLEPARTITIONSCHEME_H
30
31#include <IOKit/IOTypes.h>
32
33/*
34 * kIOApplePartitionSchemeClass is the name of the IOApplePartitionScheme class.
35 */
36
37#define kIOApplePartitionSchemeClass "IOApplePartitionScheme"
38
39/*
40 * Apple Partition Map Definitions
41 */
42
43#pragma pack(push, 1)                        /* (enable 8-bit struct packing) */
44
45/* Structure constants. */
46
47#define DPISTRLEN 32
48
49/* Partition map entry, as found in blocks 1 to dpme_map_entries of the disk. */
50
51typedef struct dpme
52{
53    UInt16  dpme_signature;       /* (unique value for partition entry, 'PM') */
54    UInt16  dpme_reserved_1;      /* (reserved for future use)                */
55    UInt32  dpme_map_entries;     /* (number of partition entries)            */
56    UInt32  dpme_pblock_start;    /* (physical block start of partition)      */
57    UInt32  dpme_pblocks;         /* (physical block count of partition)      */
58    char    dpme_name[DPISTRLEN]; /* (name of partition)                      */
59    char    dpme_type[DPISTRLEN]; /* (type of partition, eg. Apple_HFS)       */
60    UInt32  dpme_lblock_start;    /* (logical block start of partition)       */
61    UInt32  dpme_lblocks;         /* (logical block count of partition)       */
62    UInt32  dpme_flags;           /* (partition flags, see defines below)     */
63    UInt32  dpme_boot_block;      /* (logical block start of boot code)       */
64    UInt32  dpme_boot_bytes;      /* (byte count of boot code)                */
65    UInt32  dpme_load_addr;       /* (load address in memory of boot code)    */
66    UInt32  dpme_load_addr_2;     /* (reserved for future use)                */
67    UInt32  dpme_goto_addr;       /* (jump address in memory of boot code)    */
68    UInt32  dpme_goto_addr_2;     /* (reserved for future use)                */
69    UInt32  dpme_checksum;        /* (checksum of boot code)                  */
70    UInt8   dpme_process_id[16];  /* (processor type)                         */
71    UInt32  dpme_reserved_2[32];  /* (reserved for future use)                */
72    UInt32  dpme_reserved_3[62];  /* (reserved for future use)                */
73} DPME;
74
75/* Driver descriptor map entry. */
76
77typedef struct DDMap
78{
79    UInt32  ddBlock;              /* (driver's block start, sbBlkSize-blocks) */
80    UInt16  ddSize;               /* (driver's block count, 512-blocks)       */
81    UInt16  ddType;               /* (driver's system type)                   */
82} DDMap;
83
84/* Driver descriptor map, as found in block zero of the disk. */
85
86typedef struct Block0
87{
88    UInt16  sbSig;                     /* (unique value for block zero, 'ER') */
89    UInt16  sbBlkSize;                 /* (block size for this device)        */
90    UInt32  sbBlkCount;                /* (block count for this device)       */
91    UInt16  sbDevType;                 /* (device type)                       */
92    UInt16  sbDevId;                   /* (device id)                         */
93    UInt32  sbDrvrData;                /* (driver data)                       */
94    UInt16  sbDrvrCount;               /* (driver descriptor count)           */
95    DDMap   sbDrvrMap[8];              /* (driver descriptor table)           */
96    UInt8   sbReserved[430];           /* (reserved for future use)           */
97} Block0;
98
99/* Partition map signature (sbSig). */
100
101#define BLOCK0_SIGNATURE 0x4552
102
103/* Partition map entry signature (dpme_signature). */
104
105#define DPME_SIGNATURE 0x504D
106
107/* Partition map entry flags (dpme_flags). */
108
109#define DPME_FLAGS_VALID          0x00000001                   /* (bit 0)     */
110#define DPME_FLAGS_ALLOCATED      0x00000002                   /* (bit 1)     */
111#define DPME_FLAGS_IN_USE         0x00000004                   /* (bit 2)     */
112#define DPME_FLAGS_BOOTABLE       0x00000008                   /* (bit 3)     */
113#define DPME_FLAGS_READABLE       0x00000010                   /* (bit 4)     */
114#define DPME_FLAGS_WRITABLE       0x00000020                   /* (bit 5)     */
115#define DPME_FLAGS_OS_PIC_CODE    0x00000040                   /* (bit 6)     */
116#define DPME_FLAGS_OS_SPECIFIC_2  0x00000080                   /* (bit 7)     */
117#define DPME_FLAGS_OS_SPECIFIC_1  0x00000100                   /* (bit 8)     */
118#define DPME_FLAGS_RESERVED_2     0xFFFFFE00                   /* (bit 9..31) */
119
120#pragma pack(pop)                        /* (reset to default struct packing) */
121
122#ifdef KERNEL
123#ifdef __cplusplus
124
125/*
126 * Kernel
127 */
128
129#include <IOKit/storage/IOPartitionScheme.h>
130
131/*
132 * Class
133 */
134
135class IOApplePartitionScheme : public IOPartitionScheme
136{
137    OSDeclareDefaultStructors(IOApplePartitionScheme);
138
139protected:
140
141    struct ExpansionData { /* */ };
142    ExpansionData * _expansionData;
143
144    OSSet * _partitions;    /* (set of media objects representing partitions) */
145
146    /*
147     * Free all of this object's outstanding resources.
148     */
149
150    virtual void free(void);
151
152    /*
153     * Scan the provider media for an Apple partition map.  Returns the set
154     * of media objects representing each of the partitions (the retain for
155     * the set is passed to the caller), or null should no partition map be
156     * found.  The default probe score can be adjusted up or down, based on
157     * the confidence of the scan.
158     */
159
160    virtual OSSet * scan(SInt32 * score);
161
162    /*
163     * Ask whether the given partition appears to be corrupt.  A partition that
164     * is corrupt will cause the failure of the Apple partition map recognition
165     * altogether.
166     */
167
168    virtual bool isPartitionCorrupt( dpme * partition,
169                                     UInt32 partitionID,
170                                     UInt32 partitionBlockSize );
171
172    /*
173     * Ask whether the given partition appears to be invalid.  A partition that
174     * is invalid will cause it to be skipped in the scan, but will not cause a
175     * failure of the Apple partition map recognition.
176     */
177
178    virtual bool isPartitionInvalid( dpme * partition,
179                                     UInt32 partitionID,
180                                     UInt32 partitionBlockSize );
181
182    /*
183     * Instantiate a new media object to represent the given partition.
184     */
185
186    virtual IOMedia * instantiateMediaObject( dpme * partition,
187                                              UInt32 partitionID,
188                                              UInt32 partitionBlockSize );
189
190    /*
191     * Allocate a new media object (called from instantiateMediaObject).
192     */
193
194    virtual IOMedia * instantiateDesiredMediaObject(
195                                                    dpme * partition,
196                                                    UInt32 partitionID,
197                                                    UInt32 partitionBlockSize );
198
199#ifndef __LP64__
200    /*
201     * Attach the given media object to the device tree plane.
202     */
203
204    virtual bool attachMediaObjectToDeviceTree(IOMedia * media) __attribute__ ((deprecated));
205
206    /*
207     * Detach the given media object from the device tree plane.
208     */
209
210    virtual void detachMediaObjectFromDeviceTree(IOMedia * media) __attribute__ ((deprecated));
211#endif /* !__LP64__ */
212
213public:
214
215    /*
216     * Initialize this object's minimal state.
217     */
218
219    virtual bool init(OSDictionary * properties = 0);
220
221    /*
222     * Determine whether the provider media contains an Apple partition map.
223     */
224
225    virtual IOService * probe(IOService * provider, SInt32 * score);
226
227    /*
228     * Publish the new media objects which represent our partitions.
229     */
230
231    virtual bool start(IOService * provider);
232
233    /*
234     * Clean up after the media objects we published before terminating.
235     */
236
237    virtual void stop(IOService * provider);
238
239    /*
240     * Request that the provider media be re-scanned for partitions.
241     */
242
243    virtual IOReturn requestProbe(IOOptionBits options);
244
245    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  0);
246    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  1);
247    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  2);
248    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  3);
249    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  4);
250    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  5);
251    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  6);
252    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  7);
253    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  8);
254    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme,  9);
255    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme, 10);
256    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme, 11);
257    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme, 12);
258    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme, 13);
259    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme, 14);
260    OSMetaClassDeclareReservedUnused(IOApplePartitionScheme, 15);
261};
262
263#endif /* __cplusplus */
264#endif /* KERNEL */
265#endif /* !_IOAPPLEPARTITIONSCHEME_H */
266