225d48b7bc87eacb492fda0df1be98553283fabd
[bootcd.git] / conf_files / pl_hwinit
1 #!/usr/bin/python
2
3 import sys
4 import pypci
5 import pypcimap
6 import os
7 import time
8
9 loadedmodules = None
10
11 def modprobe(module, args = ""):
12     ret = os.system("/sbin/modprobe %s %s" % (module, args))
13     if os.WEXITSTATUS(ret) == 0:
14         globals()['loadedmodules'].write("%s\n" % module)
15         return True
16     else:
17         return False
18
19 def main(argv):
20     if len(argv) == 0:
21         kernel = os.uname()[2]
22     else:
23         kernel = argv[0]
24
25     if os.path.exists(kernel):
26         path = kernel
27     else:
28         path = "/lib/modules/%s/modules.pcimap" % kernel
29
30     pcimap = pypcimap.PCIMap(path)
31     print "pl_hwinit: loading applicable modules"
32     devices = pypci.get_devices()
33     storage_devices = 0
34     network_devices = 0
35     missing = []
36     globals()['loadedmodules'] = file('/tmp/loadedmodules', 'w')
37     for (slot, dev) in devices.iteritems():
38         modules = pcimap.get(dev)
39         base = (dev[4] & 0xff0000) >> 16
40         if len(modules) == 0:
41             if base == 0x01 or base == 0x02:
42                 # storage or network device, in that order
43                 missing.append((slot, dev))
44         else:
45             if base == 0x01:
46                 storage_devices += 1
47             elif base == 0x02:
48                 network_devices += 1
49
50             # FIXME: This needs improved logic in the case of multiple matching modules
51             for module in modules:
52                 print "pl_hwinit: found and loading module %s (%s)" % (module, slot)
53                 modprobe(module)
54
55     if network_devices == 0:
56         print "pl_hwinit: no supported network devices found!"
57         print "pl_hwinit: the following devices were found, but have no driver:"
58         print "pl_hwinit: ", "\npl_hwinit: ".join(missing)
59
60     # XXX: could check for storage devices too, but older kernels have a lot of that built-in
61
62     # sd_mod won't get loaded automatically
63     print "pl_hwinit: loading sd_mod"
64     modprobe("sd_mod")
65
66     # load usb_storage to support node conf files on flash disks
67     print "pl_hwinit: loading usb_storage"
68     modprobe("usb_storage")
69
70     print "pl_hwinit: loading floppy device driver"
71     modprobe("floppy","floppy=0,allowed_drive_mask")
72
73     # always wait a bit between loading the usb drivers, and checking /sys/
74     # for usb devices (this isn't necessarily for waiting for mass storage files,
75     # that is done below)
76     print "pl_hwinit: waiting for usb system to initialize."
77     time.sleep(10)
78
79     # sometimes, flash devices take a while to initialize. in fact, the kernel
80     # intentionally waits 5 seconds for a device to 'settle'. some take even longer
81     # to show up. if there are any mass storage devices on the system, try to
82     # delay until they come online, up to a max delay of 30s.
83
84     # the way this will be done is to look for files in /sys/devices that are named
85     # 'bInterfaceClass', these will be a list of the usb devices on the system, and
86     # their primary usb device interface class ids. The base directory these files 
87     # exist in will be the full path to the /sys/device entry for that device.
88     # for each mass storage devices (they have an interface class value of 08),
89     # we wait for a new symbolic link named 'driver' to exist in that directory,
90     # indicating the kernel loaded a driver for that device.
91
92     # usb interface class id for mass storage
93     INTERFACE_CLASS_MASS_STORAGE = "08"
94
95     # how long to wait in seconds before continuing on if devices
96     # aren't available
97     MAX_USB_WAIT_TIME = 30
98
99     # low long in seconds to wait between checks
100     PER_CHECK_USB_WAIT_TIME = 5
101
102
103     # find out if the device identified by the /sys dir has a module
104     # loaded for it. check for a symlink in the dir named driver.
105     def does_device_dir_have_driver(device):
106         return os.path.exists(os.path.join(device, "driver"))
107
108     def filter_and_add(list, directory, files):
109         if ("bInterfaceClass" in files and
110             int(file(os.path.join(directory, "bInterfaceClass")).read(), 16) == INTERFACE_CLASS_MASS_STORAGE):
111             list.append(directory)
112
113     wait_dev_list = []
114     os.path.walk("/sys/devices", filter_and_add, wait_dev_list)
115
116     if len(wait_dev_list) > 0:
117         print "pl_hwinit: found USB mass storage device(s). Attempting to wait"
118         print "pl_hwinit: up to %d seconds for them to come online." % MAX_USB_WAIT_TIME
119
120         total_wait_time = 0
121         success = False
122         while total_wait_time < MAX_USB_WAIT_TIME:    
123             total_wait_time += PER_CHECK_USB_WAIT_TIME
124
125             print "pl_hwinit: waiting %d seconds." % PER_CHECK_USB_WAIT_TIME
126             time.sleep(PER_CHECK_USB_WAIT_TIME)
127
128             all_devices_online = True
129             for device in wait_dev_list:
130                 if not does_device_dir_have_driver(device):
131                     all_devices_online = False
132
133             if all_devices_online:
134                 success = True
135                 print "pl_hwinit: looks like the devices are now online."
136                 break
137             else:
138                 print "pl_hwinit: not all devices online yet, waiting..."
139
140         if success:
141             print "pl_hwinit: Succesfully waited for USB mass storage devices"
142             print "pl_hwinit: to come online."
143         else:
144             print "pl_hwinit: One or more USB mass storage devices did not"
145             print "pl_hwinit: initialize in time. Continuing anyway."
146
147 if __name__ == "__main__":
148     main(sys.argv[1:])