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