Port to 1.99 extras build system.
[grub-extras.git] / osdetect.lua
bloba264e61eb6ddce7101e0063f78e280dd813481cd
1 #!lua
2 --
3 -- Copyright (C) 2009 Free Software Foundation, Inc.
4 --
5 -- GRUB is free software: you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation, either version 3 of the License, or
8 -- (at your option) any later version.
9 --
10 -- GRUB is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 function enum_device (device, fs, uuid)
20 local root
21 local title
22 local source
23 local kernels = {}
24 local kernelnames = {}
25 local kernel_num = 0
27 local function enum_file (name)
28 local version
30 version = string.match (name, "vmlinuz%-(.*)")
31 if (version == nil) then
32 version = string.match (name, "linux%-(.*)")
33 end
34 if (version ~= nil) then
35 table.insert (kernels, version)
36 table.insert (kernelnames, name)
37 kernel_num = kernel_num + 1
38 end
39 end
41 local function sort_kernel (first, second)
42 local a1, a2, a3, a4, b1, b2, b3, b4
44 a1, a2, a3, a4 = string.match (first, "(%d+)%.?(%d*).?(%d*)%-?(%d*)")
45 b1, b2, b3, b4 = string.match (second, "(%d+)%.?(%d*).?(%d*)%-?(%d*)")
46 return (a1 > b1) or (a2 > b2) or (a3 > b3) or (a4 < b4);
47 end
48 local function freebsd_variants (title, header, footer)
49 normal = "\nset FreeBSD.acpi_load=YES" ..
50 "\nset FreeBSD.hint.acpi.0.disabled=0"
51 noacpi = "\nunset FreeBSD.acpi_load" ..
52 "\nset FreeBSD.hint.acpi.0.disabled=1" ..
53 "\nset FreeBSD.loader.acpi_disabled_by_user=1"
54 safe = "\nset FreeBSD.hint.apic.0.disabled=1" ..
55 "\nset FreeBSD.hw.ata.ata_dma=0" ..
56 "\nset FreeBSD.hw.ata.atapi_dma=0" ..
57 "\nset FreeBSD.hw.ata.wc=0" ..
58 "\nset FreeBSD.hw.eisa_slots=0" ..
59 "\nset FreeBSD.hint.kbdmux.0.disabled=1"
60 grub.add_menu (header .. normal .. footer, title)
61 grub.add_menu (header .. " --single" .. normal .. footer,
62 title .. " (single)")
63 grub.add_menu (header .. " --verbose" .. normal .. footer,
64 title .. " (verbose)")
65 grub.add_menu (header .. " --verbose" .. noacpi .. footer,
66 title .. " (without ACPI)")
67 grub.add_menu (header .. " --verbose" .. noacpi .. safe .. footer,
68 title .. " (safe mode)")
69 end
71 root = "(" .. device .. ")/"
72 source = "set root=" .. device .. "\nchainloader +1"
74 local drive_num = string.match (device, "hd(%d+)")
75 if (drive_num ~= nil) and (drive_num ~= "0") then
76 source = source .. "\ndrivemap -s hd0 hd" .. drive_num
77 end
79 title = nil
80 if (grub.file_exist (root .. "bootmgr") and
81 grub.file_exist (root .. "boot/bcd")) then
82 title = "Windows Vista bootmgr"
83 source = "set root=" .. device .. "\nchainloader +1"
84 elseif (grub.file_exist (root .. "ntldr") and
85 grub.file_exist (root .. "ntdetect.com") and
86 grub.file_exist (root .. "boot.ini")) then
87 title = "Windows NT/2000/XP loader"
88 elseif (grub.file_exist (root .. "windows/win.com")) then
89 title = "Windows 98/ME"
90 elseif (grub.file_exist (root .. "io.sys") and
91 grub.file_exist (root .. "command.com")) then
92 title = "MS-DOS"
93 elseif (grub.file_exist (root .. "kernel.sys")) then
94 title = "FreeDOS"
95 elseif ((fs == "ufs1" or fs == "ufs2") and grub.file_exist (root .. "boot/kernel/kernel") and
96 grub.file_exist (root .. "boot/device.hints")) then
97 header = "set root=" .. device .. "\nfreebsd /boot/kernel/kernel"
98 footer = "\nset FreeBSD.vfs.root.mountfrom=ufs:ufsid/" .. uuid ..
99 "\nfreebsd_loadenv /boot/device.hints"
100 title = "FreeBSD (on " .. fs .. " ".. device .. ")"
101 freebsd_variants (title, header, footer)
102 return 0
103 elseif (fs == "zfs" and grub.file_exist (root .. "/@/boot/kernel/kernel") and
104 grub.file_exist (root .. "/@/boot/device.hints")) then
105 header = "set root=" .. device .. "\nfreebsd /@/boot/kernel/kernel"
106 footer = "\nfreebsd_module_elf /@/boot/kernel/opensolaris.ko" ..
107 "\nfreebsd_module_elf /@/boot/kernel/zfs.ko" ..
108 "\nfreebsd_module /@/boot/zfs/zpool.cache type=/boot/zfs/zpool.cache" ..
109 "\nprobe -l -s name $root" ..
110 "\nset FreeBSD.vfs.root.mountfrom=zfs:$name" ..
111 "\nfreebsd_loadenv /@/boot/device.hints"
112 title = "FreeBSD (on " .. fs .. " ".. device .. ")"
113 freebsd_variants (title, header, footer)
114 return 0
115 elseif (fs == "hfsplus" and grub.file_exist (root .. "mach_kernel")) then
116 source = "set root=" .. device ..
117 "\ninsmod vbe" ..
118 "\ndo_resume=0" ..
119 "\nif [ /var/vm/sleepimage -nt10 / ]; then" ..
120 "\n if xnu_resume /var/vm/sleepimage; then" ..
121 "\n do_resume=1" ..
122 "\n fi" ..
123 "\nfi" ..
124 "\nif [ $do_resume == 0 ]; then" ..
125 "\n xnu_uuid " .. uuid .. " uuid" ..
126 "\n if [ -f /Extra/DSDT.aml ]; then" ..
127 "\n acpi -e /Extra/DSDT.aml" ..
128 "\n fi" ..
129 "\n xnu_kernel /mach_kernel boot-uuid=${uuid} rd=*uuid" ..
130 "\n if [ /System/Library/Extensions.mkext -nt /System/Library/Extensions ]; then" ..
131 "\n xnu_mkext /System/Library/Extensions.mkext" ..
132 "\n else" ..
133 "\n xnu_kextdir /System/Library/Extensions" ..
134 "\n fi" ..
135 "\n if [ -f /Extra/Extensions.mkext ]; then" ..
136 "\n xnu_mkext /Extra/Extensions.mkext" ..
137 "\n fi" ..
138 "\n if [ -d /Extra/Extensions ]; then" ..
139 "\n xnu_kextdir /Extra/Extensions" ..
140 "\n fi" ..
141 "\n if [ -f /Extra/devtree.txt ]; then" ..
142 "\n xnu_devtree /Extra/devtree.txt" ..
143 "\n fi" ..
144 "\n if [ -f /Extra/splash.jpg ]; then" ..
145 "\n insmod jpeg" ..
146 "\n xnu_splash /Extra/splash.jpg" ..
147 "\n fi" ..
148 "\n if [ -f /Extra/splash.png ]; then" ..
149 "\n insmod png" ..
150 "\n xnu_splash /Extra/splash.png" ..
151 "\n fi" ..
152 "\n if [ -f /Extra/splash.tga ]; then" ..
153 "\n insmod tga" ..
154 "\n xnu_splash /Extra/splash.tga" ..
155 "\n fi" ..
156 "\nfi"
157 title = "Mac OS X/Darwin"
158 else
159 grub.enum_file (enum_file, root .. "boot")
160 if kernel_num ~= 0 then
161 table.sort (kernels, sort_kernel)
162 table.sort (kernelnames, sort_kernel)
163 for i = 1, kernel_num do
164 local initrd
166 title = "Linux " .. kernels[i]
167 source = "set root=" .. device ..
168 "\nlinux /boot/" .. kernelnames[i] ..
169 " root=UUID=" .. uuid .. " ro"
171 if grub.file_exist (root .. "boot/initrd-" ..
172 kernels[i] .. ".img") then
173 initrd = "\ninitrd /boot/initrd-" .. kernels[i] .. ".img"
174 elseif grub.file_exist (root .. "boot/initrd.img-" .. kernels[i]) then
175 initrd = "\ninitrd /boot/initrd.img-" .. kernels[i]
176 elseif grub.file_exist (root .. "boot/initrd-" .. kernels[i]) then
177 initrd = "\ninitrd /boot/initrd-" .. kernels[i]
178 else
179 initrd = ""
182 grub.add_menu (source .. initrd, title)
183 grub.add_menu (source .. " single" .. initrd,
184 title .. " (single-user mode)")
186 return 0
190 if title == nil then
191 local partition = string.match (device, ".*,(%d+)")
193 if (partition ~= nil) and (tonumber (partition) > 4) then
194 return 0
197 title = "Other OS"
200 grub.add_menu (source, title)
201 return 0
204 grub.enum_device (enum_device)