The Pedigree Project  0.1
modules/drivers/common/partition/main.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 "PartitionService.h"
21 #include "apple.h"
22 #include "modules/Module.h"
23 #include "msdos.h"
24 #include "pedigree/kernel/Service.h"
25 #include "pedigree/kernel/ServiceFeatures.h"
26 #include "pedigree/kernel/ServiceManager.h"
27 #include "pedigree/kernel/machine/Device.h"
28 #include "pedigree/kernel/machine/Disk.h"
29 #include "pedigree/kernel/processor/types.h"
30 #include "pedigree/kernel/utilities/String.h"
31 #include "pedigree/kernel/utilities/utility.h"
32 
33 static Service *pService = 0;
34 static ServiceFeatures *pFeatures = 0;
35 
36 static bool probeDevice(Disk *pDev)
37 {
38  // Does the disk have an MS-DOS partition table?
39  if (msdosProbeDisk(pDev))
40  return true;
41 
42  // No? how about an Apple_Map?
43  if (appleProbeDisk(pDev))
44  return true;
45 
46  // Oh well, better luck next time.
47  return false;
48 }
49 
50 static Device *checkNode(Device *pDev)
51 {
52  bool hasPartitions = false;
53  if (pDev->getType() == Device::Disk)
54  {
55  // Check that none of its children are Partitions
56  // (in which case we've probed this before!)
57  for (unsigned int i = 0; i < pDev->getNumChildren(); i++)
58  {
59  String name;
60  pDev->getChild(i)->getName(name);
61  if (!StringCompare(name, "msdos-partition") ||
62  !StringCompare(name, "apple-partition"))
63  {
64  hasPartitions = true;
65  break;
66  }
67  }
68 
69  if (!hasPartitions)
70  probeDevice(static_cast<Disk *>(pDev));
71  }
72 
73  return pDev;
74 }
75 
77  ServiceFeatures::Type type, void *pData, size_t dataLen)
78 {
79  // Correct type?
80  if (pFeatures->provides(type))
81  {
82  // We only provide Touch services
83  if (type & ServiceFeatures::touch)
84  {
85  Disk *pDisk = static_cast<Disk *>(pData);
86  return probeDevice(pDisk);
87  }
88  }
89 
90  // Not provided by us, fail!
91  return false;
92 }
93 
94 static bool entry()
95 {
96  // Install the Partition Service
97  pService = new PartitionService;
98  pFeatures = new ServiceFeatures;
99  pFeatures->add(ServiceFeatures::touch);
100  ServiceManager::instance().addService(
101  String("partition"), pService, pFeatures);
102 
103  // Walk the device tree looking for disks that don't have "partition"
104  // children.
105  Device::foreach (checkNode);
106 
107  // Never fail, even if no partitions found. The partition service is still
108  // critical to the system.
109  return true;
110 }
111 
112 static void exit()
113 {
114  ServiceManager::instance().removeService(String("partition"));
115  delete pService;
116  delete pFeatures;
117 }
118 
119 #if defined(ARM_COMMON) // No ATA controller
120 MODULE_INFO("partition", &entry, &exit);
121 #elif defined(HOSTED)
122 MODULE_INFO("partition", &entry, &exit, "diskimage");
123 #else
124 MODULE_INFO("partition", &entry, &exit, "ata");
125 #endif
Device * getChild(size_t n)
Definition: Device.cc:132
size_t getNumChildren()
Definition: Device.cc:137
virtual void getName(String &str)
Definition: Device.cc:121
A disk device - a block device in UNIX terms.
Definition: Device.h:54
void removeService(const String &serviceName)
Definition: String.h:49
virtual bool provides(Type service)
Definition: Disk.h:32
Definition: Device.h:43
bool serve(ServiceFeatures::Type type, void *pData, size_t dataLen)
static void foreach(Callback callback, Device *root=0)
Definition: Device.cc:94
virtual Type getType()
Definition: Device.h:163
void addService(const String &serviceName, Service *s, ServiceFeatures *feats)
virtual void add(Type s)
bool msdosProbeDisk(Disk *pDisk)
Definition: msdos.cc:457