1c94634f2c579946f1174dc996dc2fa9b95aad09
[openwrt.git] / target / linux / x86 / image / Makefile
1
2 # Copyright (C) 2006-2010 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 GRUB_TERMINALS =
13 GRUB_SERIAL_CONFIG =
14 GRUB_TERMINAL_CONFIG =
15 GRUB_CONSOLE_CMDLINE =
16
17 ifneq ($(CONFIG_X86_GRUB_CONSOLE),)
18   GRUB_CONSOLE_CMDLINE += console=tty0
19   GRUB_TERMINALS += console
20 endif
21
22 ifneq ($(CONFIG_X86_GRUB_SERIAL),)
23   GRUB_CONSOLE_CMDLINE += console=$(call qstrip,$(CONFIG_X86_GRUB_SERIAL)),$(CONFIG_X86_GRUB_BAUDRATE)n8
24   GRUB_SERIAL_CONFIG := serial --unit=0 --speed=$(CONFIG_X86_GRUB_BAUDRATE) --word=8 --parity=no --stop=1
25   GRUB_TERMINALS += serial
26 endif
27
28 ifneq ($(GRUB_TERMINALS),)
29   GRUB_TERMINAL_CONFIG := terminal --timeout=2 $(GRUB_TERMINALS)
30 endif
31
32
33 ifneq ($(CONFIG_X86_GRUB_IMAGES),)
34  ifneq ($(HOST_OS),Darwin)
35
36   BOOTOPTS:=$(call qstrip,$(CONFIG_X86_GRUB_BOOTOPTS))
37   ROOTPART:=$(call qstrip,$(CONFIG_X86_GRUB_ROOTPART))
38
39   define Image/cmdline/ext2
40     root=$(ROOTPART) rootfstype=ext2 rootwait
41   endef
42   
43   define Image/cmdline/jffs2-64k
44     block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootwait
45   endef
46
47   define Image/cmdline/jffs2-128k
48     block2mtd.block2mtd=$(ROOTPART),131072,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootwait
49   endef
50
51   define Image/cmdline/squashfs
52     block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=squashfs rootwait
53   endef
54
55   define Image/Build/grub
56         # left here because the image builder doesnt need these
57         $(INSTALL_DIR) $(KDIR)/root.grub/boot/grub
58         $(CP) \
59                 $(KDIR)/*stage* \
60                 $(KDIR)/root.grub/boot/grub/
61         $(CP) $(KDIR)/bzImage $(KDIR)/root.grub/boot/vmlinuz
62         sed \
63                 -e 's#@SERIAL_CONFIG@#$(strip $(GRUB_SERIAL_CONFIG))#g' \
64                 -e 's#@TERMINAL_CONFIG@#$(strip $(GRUB_TERMINAL_CONFIG))#g' \
65                 -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1)) $(BOOTOPTS) $(GRUB_CONSOLE_CMDLINE))#g' \
66                 ./menu.lst > $(KDIR)/root.grub/boot/grub/menu.lst
67         PADDING="$(CONFIG_X86_GRUB_IMAGES_PAD)" PATH="$(TARGET_PATH)" ./gen_image_x86.sh \
68                 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
69                 $(CONFIG_X86_GRUB_KERNELPART) $(KDIR)/root.grub \
70                 $(CONFIG_TARGET_ROOTFS_FSPART) $(KDIR)/root.$(1)
71         $(call Image/Build/grub/$(1))
72   endef
73
74   define Image/Prepare/grub
75         # for the image builder
76         $(CP) \
77                 $(STAGING_DIR_HOST)/usr/lib/grub/i386-*/stage1 \
78                 $(STAGING_DIR_HOST)/usr/lib/grub/i386-*/stage2 \
79                 $(STAGING_DIR_HOST)/usr/lib/grub/i386-*/e2fs_stage1_5 \
80                 $(KDIR)/
81   endef
82
83  else
84
85   define Image/Build/grub
86         PADDING="$(CONFIG_X86_GRUB_IMAGES_PAD)" PATH="$(TARGET_PATH)" NOGRUB=1 ./gen_image_x86.sh 
87                 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
88                 $(CONFIG_X86_GRUB_KERNELPART) "" \
89                 $(CONFIG_TARGET_ROOTFS_FSPART) $(KDIR)/root.$(1)
90   endef
91
92  endif
93 endif
94
95 ROOTDELAY=10
96
97 ifneq ($(CONFIG_OLPC_BOOTSCRIPT_IMAGES),)
98
99   ROOTPART:=$(call qstrip,$(CONFIG_OLPC_BOOTSCRIPT_ROOTPART))
100
101   define Image/cmdline/ext2
102     root=$(ROOTPART) rootfstype=ext2 rootwait
103   endef
104
105   define Image/cmdline/jffs2-64k
106     block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootdelay=$(ROOTDELAY)
107   endef
108
109   define Image/cmdline/jffs2-128k
110     block2mtd.block2mtd=$(ROOTPART),131072,rootfs root=/dev/mtdblock0 rootfstype=jffs2 rootdelay=$(ROOTDELAY)
111   endef
112
113   define Image/cmdline/squashfs
114     block2mtd.block2mtd=$(ROOTPART),65536,rootfs root=/dev/mtdblock0 rootfstype=squashfs rootdelay=$(ROOTDELAY)
115   endef
116
117   define Image/Build/bootscript
118         # left here because the image builder doesnt need these
119         $(INSTALL_DIR) $(KDIR)/root.bootscript/boot
120         $(CP) $(KDIR)/bzImage $(KDIR)/root.bootscript/boot/vmlinuz
121         sed -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1))) $(BOOTOPTS)#g' \
122                 ./olpc.fth > $(KDIR)/root.bootscript/boot/olpc.fth
123         PADDING="$(CONFIG_OLPC_BOOTSCRIPT_IMAGES_PAD)" PATH="$(TARGET_PATH)" ./gen_image_olpc.sh \
124                 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
125                 $(CONFIG_OLPC_BOOTSCRIPT_KERNELPART) $(KDIR)/root.bootscript \
126                 $(CONFIG_TARGET_ROOTFS_FSPART) $(KDIR)/root.$(1)
127   endef
128
129 endif
130
131 define Image/Build/squashfs
132         $(call prepare_generic_squashfs,$(KDIR)/root.squashfs)
133 endef
134
135 define Image/Build/iso 
136         $(INSTALL_DIR) $(KDIR)/root.grub/boot/grub
137         $(CP) \
138                 $(STAGING_DIR_HOST)/usr/lib/grub/i386-openwrt/stage2_eltorito \
139                 $(KDIR)/root.grub/boot/grub/stage2_eltorito 
140         sed \
141                 -e 's#@SERIAL_CONFIG@#$(strip $(GRUB_SERIAL_CONFIG))#g' \
142                 -e 's#@TERMINAL_CONFIG@#$(strip $(GRUB_TERMINAL_CONFIG))#g' \
143                 -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1)) $(BOOTOPTS) $(GRUB_CONSOLE_CMDLINE))#g' \
144                 -e 's#(hd0,0)#(cd)#g' \
145                 ./menu.lst > $(KDIR)/root.grub/boot/grub/menu.lst
146         $(CP) $(KDIR)/bzImage $(KDIR)/root.grub/boot/vmlinuz 
147         mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table \
148                 -o $(KDIR)/root.iso $(KDIR)/root.grub 
149 endef 
150
151 ifneq ($(CONFIG_X86_VDI_IMAGES),)
152   define Image/Build/vdi
153     # left here because the image builder doesnt need these
154     ifeq ($(1),ext2)
155                 rm $(BIN_DIR)/$(IMG_PREFIX)-ext2.vdi || true
156                 VBoxManage convertfromraw -format VDI \
157                         $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
158                         $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vdi
159                 # XXX: VBoxManage insists on setting perms to 0600
160                 chmod 0644 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vdi
161     endif
162   endef
163 endif
164
165 ifneq ($(CONFIG_X86_VMDK_IMAGES),)
166   define Image/Build/vmdk
167     # left here because the image builder doesnt need these
168     ifeq ($(1),ext2)
169                 rm $(BIN_DIR)/$(IMG_PREFIX)-ext2.vmdk || true
170                 qemu-img convert -f raw -O vmdk \
171                         $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
172                         $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).vmdk
173     endif
174   endef
175 endif
176
177 define Image/BuildKernel
178         $(CP) $(KDIR)/bzImage $(BIN_DIR)/$(IMG_PREFIX)-vmlinuz
179 endef
180
181 define Image/Prepare
182         $(CP) $(LINUX_DIR)/arch/x86/boot/bzImage $(KDIR)/bzImage
183         $(call Image/Prepare/bootscript)
184         $(call Image/Prepare/grub)
185 endef
186
187 define Image/Build
188         $(call Image/Build/$(1))
189         $(call Image/Build/bootscript,$(1))
190   ifneq ($(1),iso)
191         $(call Image/Build/grub,$(1))
192         $(call Image/Build/vdi,$(1))
193         $(call Image/Build/vmdk,$(1))
194   endif
195         $(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-rootfs-$(1).img
196         $(CP) $(KDIR)/bzImage $(BIN_DIR)/$(IMG_PREFIX)-vmlinuz
197   ifeq ($(1),ext2)
198         gzip -f9 $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img
199         gzip -f9 $(BIN_DIR)/$(IMG_PREFIX)-rootfs-$(1).img
200   endif
201 endef
202
203 $(eval $(call BuildImage))
204
205 ifneq ($(CONFIG_X86_VDI_IMAGES),)
206   $(eval $(call RequireCommand,VBoxManage, \
207         You need VBoxManage to generate VirtualBox images. \
208   ))
209 endif
210
211 ifneq ($(CONFIG_X86_VMDK_IMAGES),)
212   $(eval $(call RequireCommand,qemu-img, \
213         You need qemu-img to generate VMware images. \
214   ))
215 endif