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 "Partition.h"
22 #include "pedigree/kernel/Log.h"
23 #include "pedigree/kernel/machine/Disk.h"
24 #include "pedigree/kernel/processor/types.h"
25 #include "pedigree/kernel/utilities/String.h"
26 #include "pedigree/kernel/utilities/utility.h"
27 
28 class Device;
29 
30 bool appleProbeDisk(Disk *pDisk)
31 {
32  // Read the second sector (512 bytes) of the disk into a buffer.
33  uintptr_t buff;
34  if ((buff = pDisk->read(512ULL)) == 0)
35  {
36  WARNING("Disk read failure during Apple partition table search.");
37  return false;
38  }
39 
40  ApplePartitionMap *pMap = reinterpret_cast<ApplePartitionMap *>(buff);
41 
42  String diskName;
43  pDisk->getName(diskName);
44 
45  // Check for the magic signature.
46  if (pMap->pmSig != BIG_TO_HOST16(APPLE_PM_SIG))
47  {
48  NOTICE("Apple partition map not found on disk " << diskName);
49  return false;
50  }
51 
52  NOTICE("Apple partition map found on disk " << diskName);
53 
54  // Cache the number of partition table entries.
55  size_t nEntries = BIG_TO_HOST32(pMap->pmMapBlkCnt);
56  for (size_t i = 0; i < nEntries; i++)
57  {
58  if (i > 0) // We don't need to load anything in for the first pmap -
59  // already done!
60  {
61  if ((buff = pDisk->read(512ULL + i * 512ULL)) == 0)
62  {
63  WARNING(
64  "Disk read failure during partition table recognition.");
65  return false;
66  }
67  pMap = reinterpret_cast<ApplePartitionMap *>(buff);
68  }
69 
70  NOTICE(
71  "Detected partition '" << pMap->pmPartName << "', type '"
72  << pMap->pmParType << "'");
73 
74  // Create a partition object.
75  Partition *pObj = new Partition(
76  String(pMap->pmParType),
77  static_cast<uint64_t>(BIG_TO_HOST32(pMap->pmPyPartStart)) * 512ULL,
78  static_cast<uint64_t>(BIG_TO_HOST32(pMap->pmPartBlkCnt)) * 512ULL);
79  pObj->setParent(static_cast<Device *>(pDisk));
80  pDisk->addChild(static_cast<Device *>(pObj));
81  }
82  return true;
83 }
Definition: String.h:49
Definition: Disk.h:32
Definition: Device.h:43
#define WARNING(text)
Definition: Log.h:78
virtual uintptr_t read(uint64_t location)
Definition: Disk.cc:56
#define NOTICE(text)
Definition: Log.h:74
void addChild(Device *pDevice)
Definition: Device.cc:127
void setParent(Device *p)
Definition: Device.h:154
virtual void getName(String &str)
Definition: Disk.cc:46