x86: add support for building grub2 images
[openwrt/svn-archive/archive.git] / target / linux / x86 / image / Makefile
1 #
2 # Copyright (C) 2006-2012 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7 include $(TOPDIR)/rules.mk
8 include $(INCLUDE_DIR)/image.mk
9
10 export PATH=$(TARGET_PATH):/sbin
11
12 GRUB2_MODULES = at_keyboard biosdisk boot chain configfile ext2 linux ls part_msdos reboot serial vga
13 GRUB_TERMINALS =
14 GRUB_SERIAL_CONFIG =
15 GRUB_TERMINAL_CONFIG =
16 GRUB_CONSOLE_CMDLINE =
17
18 ifneq ($(CONFIG_X86_GRUB_CONSOLE),)
19 GRUB_CONSOLE_CMDLINE += console=tty0
20 GRUB_TERMINALS += console
21 endif
22
23 ifneq ($(CONFIG_X86_GRUB_SERIAL),)
24 GRUB_CONSOLE_CMDLINE += console=$(call qstrip,$(CONFIG_X86_GRUB_SERIAL)),$(CONFIG_X86_GRUB_BAUDRATE)n8
25 GRUB_SERIAL_CONFIG := serial --unit=0 --speed=$(CONFIG_X86_GRUB_BAUDRATE) --word=8 --parity=no --stop=1
26 GRUB_TERMINALS += serial
27 endif
28
29 ifneq ($(GRUB_TERMINALS),)
30 ifneq ($(CONFIG_X86_USE_GRUB2),)
31 GRUB_TERMINAL_CONFIG := terminal_input $(GRUB_TERMINALS); terminal_output $(GRUB_TERMINALS)
32 else
33 GRUB_TERMINAL_CONFIG := terminal --timeout=2 $(GRUB_TERMINALS)
34 endif
35 endif
36
37 ROOTPART:=$(call qstrip,$(CONFIG_TARGET_ROOTFS_PARTNAME))
38
39 GRUB_TIMEOUT:=$(call qstrip,$(CONFIG_X86_GRUB_TIMEOUT))
40
41 ifneq ($(CONFIG_X86_GRUB_IMAGES),)
42
43 BOOTOPTS:=$(call qstrip,$(CONFIG_X86_GRUB_BOOTOPTS))
44
45 define Image/cmdline/ext4
46 root=$(ROOTPART) rootfstype=ext4 rootwait
47 endef
48
49 define Image/cmdline/jffs2-64k
50 block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootwait
51 endef
52
53 define Image/cmdline/jffs2-128k
54 block2mtd.block2mtd=$(ROOTPART),131072,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootwait
55 endef
56
57 define Image/cmdline/squashfs
58 block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=squashfs rootwait
59 endef
60
61 define Image/Build/grub
62 # left here because the image builder doesnt need these
63 $(INSTALL_DIR) $(KDIR)/root.grub/boot/grub
64 $(CP) \
65 $(KDIR)/stage1 \
66 $(KDIR)/stage2 \
67 $(KDIR)/e2fs_stage1_5 \
68 $(KDIR)/root.grub/boot/grub/
69 $(CP) $(KDIR)/bzImage $(KDIR)/root.grub/boot/vmlinuz
70 sed \
71 -e 's#@SERIAL_CONFIG@#$(strip $(GRUB_SERIAL_CONFIG))#g' \
72 -e 's#@TERMINAL_CONFIG@#$(strip $(GRUB_TERMINAL_CONFIG))#g' \
73 -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1)) $(BOOTOPTS) $(GRUB_CONSOLE_CMDLINE))#g' \
74 -e 's#@TIMEOUT@#$(GRUB_TIMEOUT)#g' \
75 ./menu.lst > $(KDIR)/root.grub/boot/grub/menu.lst
76 PADDING="$(CONFIG_TARGET_IMAGES_PAD)" PATH="$(TARGET_PATH)" ./gen_image_grub.sh \
77 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
78 $(CONFIG_TARGET_KERNEL_PARTSIZE) $(KDIR)/root.grub \
79 $(CONFIG_TARGET_ROOTFS_PARTSIZE) $(KDIR)/root.$(1)
80 $(call Image/Build/grub/$(1))
81 endef
82
83 define Image/Prepare/grub
84 # for the image builder
85 $(CP) \
86 $(STAGING_DIR)/usr/lib/grub/i386-openwrt/stage1 \
87 $(STAGING_DIR)/usr/lib/grub/i386-openwrt/stage2 \
88 $(STAGING_DIR)/usr/lib/grub/i386-openwrt/stage2_eltorito \
89 $(STAGING_DIR)/usr/lib/grub/i386-openwrt/e2fs_stage1_5 \
90 $(KDIR)/
91 endef
92
93 define Image/Build/grub2
94 # left here because the image builder doesnt need these
95 $(INSTALL_DIR) $(KDIR)/root.grub/boot/grub $(KDIR)/grub2
96 $(CP) $(KDIR)/bzImage $(KDIR)/root.grub/boot/vmlinuz
97 grub-mkimage \
98 -o $(KDIR)/grub2/core.img \
99 -O i386-pc \
100 -c ./grub-early.cfg \
101 $(GRUB2_MODULES)
102 $(CP) $(STAGING_DIR_HOST)/lib/grub/i386-pc/*.img $(KDIR)/grub2/
103 echo '(hd0) $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img' > $(KDIR)/grub2/device.map
104 sed \
105 -e 's#@SERIAL_CONFIG@#$(strip $(GRUB_SERIAL_CONFIG))#g' \
106 -e 's#@TERMINAL_CONFIG@#$(strip $(GRUB_TERMINAL_CONFIG))#g' \
107 -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1)) $(BOOTOPTS) $(GRUB_CONSOLE_CMDLINE))#g' \
108 -e 's#@TIMEOUT@#$(GRUB_TIMEOUT)#g' \
109 ./grub.cfg > $(KDIR)/root.grub/boot/grub/grub.cfg
110 PADDING="$(CONFIG_TARGET_IMAGES_PAD)" PATH="$(TARGET_PATH)" ./gen_image_generic.sh \
111 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
112 $(CONFIG_TARGET_KERNEL_PARTSIZE) $(KDIR)/root.grub \
113 $(CONFIG_TARGET_ROOTFS_PARTSIZE) $(KDIR)/root.$(1) \
114 256
115 grub-bios-setup \
116 --device-map="$(KDIR)/grub2/device.map" \
117 -d "$(KDIR)/grub2" \
118 -r "hd0,msdos1" \
119 "$(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img"
120 $(call Image/Build/grub/$(1))
121 endef
122 endif
123
124 ROOTDELAY=10
125
126 ifneq ($(CONFIG_OLPC_BOOTSCRIPT_IMAGES),)
127
128 define Image/cmdline/ext4
129 root=$(ROOTPART) rootfstype=ext4 rootwait
130 endef
131
132 define Image/cmdline/jffs2-64k
133 block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootdelay=$(ROOTDELAY)
134 endef
135
136 define Image/cmdline/jffs2-128k
137 block2mtd.block2mtd=$(ROOTPART),131072,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootdelay=$(ROOTDELAY)
138 endef
139
140 define Image/cmdline/squashfs
141 block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=squashfs rootdelay=$(ROOTDELAY)
142 endef
143
144 define Image/Build/bootscript
145 # left here because the image builder doesnt need these
146 $(INSTALL_DIR) $(KDIR)/root.bootscript/boot
147 $(CP) $(KDIR)/bzImage $(KDIR)/root.bootscript/boot/vmlinuz
148 sed -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1))) $(BOOTOPTS)#g' \
149 ./olpc.fth > $(KDIR)/root.bootscript/boot/olpc.fth
150 PADDING="$(CONFIG_TARGET_IMAGES_PAD)" PATH="$(TARGET_PATH)" ./gen_image_olpc.sh \
151 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
152 $(CONFIG_TARGET_KERNEL_PARTSIZE) $(KDIR)/root.bootscript \
153 $(CONFIG_TARGET_ROOTFS_PARTSIZE) $(KDIR)/root.$(1)
154 endef
155
156 endif
157
158 define Image/Build/squashfs
159 $(call prepare_generic_squashfs,$(KDIR)/root.squashfs)
160 endef
161
162 define Image/Build/iso
163 $(INSTALL_DIR) $(KDIR)/root.grub/boot/grub
164 $(CP) \
165 $(KDIR)/stage2_eltorito \
166 $(KDIR)/root.grub/boot/grub/
167 sed \
168 -e 's#@SERIAL_CONFIG@#$(strip $(GRUB_SERIAL_CONFIG))#g' \
169 -e 's#@TERMINAL_CONFIG@#$(strip $(GRUB_TERMINAL_CONFIG))#g' \
170 -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1)) $(BOOTOPTS) $(GRUB_CONSOLE_CMDLINE))#g' \
171 -e 's#(hd0,0)#(cd)#g' \
172 ./menu.lst > $(KDIR)/root.grub/boot/grub/menu.lst
173 $(CP) $(KDIR)/bzImage $(KDIR)/root.grub/boot/vmlinuz
174 mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table \
175 -o $(KDIR)/root.iso $(KDIR)/root.grub
176 endef
177
178 ifneq ($(CONFIG_X86_VDI_IMAGES),)
179 define Image/Build/vdi
180 # left here because the image builder doesnt need these
181 ifeq ($(1),ext4)
182 rm $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vdi || true
183 qemu-img convert -f raw -O vdi \
184 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
185 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vdi
186 # XXX: VBoxManage insists on setting perms to 0600
187 chmod 0644 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vdi
188 endif
189 endef
190 endif
191
192 ifneq ($(CONFIG_X86_VMDK_IMAGES),)
193 define Image/Build/vmdk
194 # left here because the image builder doesnt need these
195 ifeq ($(1),ext4)
196 rm $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vmdk || true
197 qemu-img convert -f raw -O vmdk \
198 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
199 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vmdk
200 endif
201 endef
202 endif
203
204 define Image/Build/gzip
205 gzip -f9 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img
206 gzip -f9 $(BIN_DIR)/$(IMG_PREFIX)-rootfs-$(1).img
207 endef
208
209 ifneq ($(CONFIG_TARGET_IMAGES_GZIP),)
210 define Image/Build/gzip/ext4
211 $(call Image/Build/gzip,ext4)
212 endef
213 ifneq ($(CONFIG_TARGET_IMAGES_PAD),)
214 define Image/Build/gzip/squashfs
215 $(call Image/Build/gzip,squashfs)
216 endef
217 define Image/Build/gzip/jffs2-64k
218 $(call Image/Build/gzip,jffs2-64k)
219 endef
220 define Image/Build/gzip/jffs2-128k
221 $(call Image/Build/gzip,jffs2-128k)
222 endef
223 endif
224 endif
225
226 define Image/BuildKernel
227 $(CP) $(KDIR)/bzImage $(BIN_DIR)/$(IMG_PREFIX)-vmlinuz
228 ifneq ($(CONFIG_X86_ETHERBOOT_IMAGES),)
229 rm -f $(BIN_DIR)/$(IMG_PREFIX)-etherboot
230 $(STAGING_DIR_HOST)/bin/mkelfImage \
231 --append=$(CONFIG_X86_ETHERBOOT_BOOTOPTS) \
232 $(KDIR)/bzImage \
233 $(BIN_DIR)/$(IMG_PREFIX)-etherboot
234 endif
235 endef
236
237 define Image/Prepare
238 $(CP) $(LINUX_DIR)/arch/x86/boot/bzImage $(KDIR)/bzImage
239 $(call Image/Prepare/bootscript)
240 $(call Image/Prepare/grub)
241 endef
242
243 define Image/Build
244 $(call Image/Build/$(1))
245 $(call Image/Build/bootscript,$(1))
246 ifneq ($(1),iso)
247 $(call Image/Build/$(if $(CONFIG_X86_USE_GRUB2),grub2,grub),$(1))
248 $(call Image/Build/vdi,$(1))
249 $(call Image/Build/vmdk,$(1))
250 endif
251 $(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-rootfs-$(1).img
252 $(CP) $(KDIR)/bzImage $(BIN_DIR)/$(IMG_PREFIX)-vmlinuz
253 $(call Image/Build/gzip/$(1))
254 endef
255
256 $(eval $(call BuildImage))