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