The Pedigree Project 0.1
apple.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "apple.h"
21#include "pedigree/kernel/Log.h"
22#include "pedigree/kernel/machine/Disk.h"
23#include "pedigree/kernel/processor/types.h"
24#include "pedigree/kernel/utilities/String.h"
25#include "pedigree/kernel/utilities/utility.h"
26
27#include "Partition.h"
28
29class Device;
30
31bool appleProbeDisk(Disk* pDisk) {
32 // Read the second sector (512 bytes) of the disk into a buffer.
33 BufferView buffer = pDisk->read(512ULL);
34 if (!buffer || buffer.size() < sizeof(ApplePartitionMap)) {
35 if (buffer) {
36 pDisk->unpin(512ULL);
37 }
38 WARNING("Disk read failure during Apple partition table search.");
39 return false;
40 }
41
43 MemoryCopy(&map, buffer.data(), sizeof(map));
44 pDisk->unpin(512ULL);
45
46 String diskName;
47 pDisk->getName(diskName);
48
49 // Check for the magic signature.
50 if (map.pmSig != BIG_TO_HOST16(APPLE_PM_SIG)) {
51 NOTICE("Apple partition map not found on disk " << diskName);
52 return false;
53 }
54
55 NOTICE("Apple partition map found on disk " << diskName);
56
57 // Cache the number of partition table entries.
58 size_t nEntries = BIG_TO_HOST32(map.pmMapBlkCnt);
59 for (size_t i = 0; i < nEntries; i++) {
60 if (i > 0) // We don't need to load anything in for the first pmap -
61 // already done!
62 {
63 const uint64_t diskLocation = 512ULL + i * 512ULL;
64 buffer = pDisk->read(diskLocation);
65 if (!buffer || buffer.size() < sizeof(ApplePartitionMap)) {
66 if (buffer) {
67 pDisk->unpin(diskLocation);
68 }
69 WARNING("Disk read failure during partition table recognition.");
70 return false;
71 }
72 MemoryCopy(&map, buffer.data(), sizeof(map));
73 pDisk->unpin(diskLocation);
74 }
75
76 NOTICE("Detected partition '" << map.pmPartName << "', type '" << map.pmParType << "'");
77
78 // Create a partition object.
79 Partition* pObj = new Partition(
80 String(map.pmParType), static_cast<uint64_t>(BIG_TO_HOST32(map.pmPyPartStart)) * 512ULL,
81 static_cast<uint64_t>(BIG_TO_HOST32(map.pmPartBlkCnt)) * 512ULL);
82 pObj->setParent(static_cast<Device*>(pDisk));
83 pDisk->addChild(static_cast<Device*>(pObj));
84 }
85 return true;
86}
void addChild(Device *pDevice)
Definition Device.cc:135
void setParent(Device *p)
Definition Device.h:170
Definition Disk.h:35
virtual BufferView read(uint64_t location)
Definition Disk.cc:163
virtual void getName(String &str)
Definition Disk.cc:155
virtual void unpin(uint64_t location)=0