image.mk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. override TARGET_BUILD=
  8. include $(INCLUDE_DIR)/prereq.mk
  9. include $(INCLUDE_DIR)/kernel.mk
  10. include $(INCLUDE_DIR)/host.mk
  11. include $(INCLUDE_DIR)/version.mk
  12. override MAKE:=$(_SINGLE)$(SUBMAKE)
  13. override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
  14. KDIR=$(KERNEL_BUILD_DIR)
  15. KDIR_TMP=$(KDIR)/tmp
  16. DTS_DIR:=$(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/dts
  17. sanitize = $(call tolower,$(subst _,-,$(1)))
  18. DIST_SANITIZED:=$(call sanitize,$(VERSION_DIST))
  19. EXTRA_NAME_SANITIZED=$(call sanitize,$(EXTRA_IMAGE_NAME))
  20. IMG_PREFIX:=$(DIST_SANITIZED)-$(if $(CONFIG_VERSION_FILENAMES),$(VERSION_NUMBER)-)$(if $(EXTRA_NAME_SANITIZED),$(EXTRA_NAME_SANITIZED)-)$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
  21. MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
  22. ifneq ($(CONFIG_BIG_ENDIAN),)
  23. JFFS2OPTS := --big-endian --squash-uids -v
  24. else
  25. JFFS2OPTS := --little-endian --squash-uids -v
  26. endif
  27. ifeq ($(CONFIG_JFFS2_RTIME),y)
  28. JFFS2OPTS += -X rtime
  29. endif
  30. ifeq ($(CONFIG_JFFS2_ZLIB),y)
  31. JFFS2OPTS += -X zlib
  32. endif
  33. ifeq ($(CONFIG_JFFS2_LZMA),y)
  34. JFFS2OPTS += -X lzma --compression-mode=size
  35. endif
  36. ifneq ($(CONFIG_JFFS2_RTIME),y)
  37. JFFS2OPTS += -x rtime
  38. endif
  39. ifneq ($(CONFIG_JFFS2_ZLIB),y)
  40. JFFS2OPTS += -x zlib
  41. endif
  42. ifneq ($(CONFIG_JFFS2_LZMA),y)
  43. JFFS2OPTS += -x lzma
  44. endif
  45. JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
  46. SQUASHFS_BLOCKSIZE := $(CONFIG_TARGET_SQUASHFS_BLOCK_SIZE)k
  47. SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
  48. SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
  49. SQUASHFSCOMP := gzip
  50. LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
  51. ifeq ($(CONFIG_SQUASHFS_LZMA),y)
  52. SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
  53. endif
  54. ifeq ($(CONFIG_SQUASHFS_XZ),y)
  55. ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
  56. BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
  57. endif
  58. SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
  59. endif
  60. JFFS2_BLOCKSIZE ?= 64k 128k
  61. fs-types-$(CONFIG_TARGET_ROOTFS_SQUASHFS) += squashfs
  62. fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addprefix jffs2-,$(JFFS2_BLOCKSIZE))
  63. fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2_NAND) += $(addprefix jffs2-nand-,$(NAND_BLOCKSIZE))
  64. fs-types-$(CONFIG_TARGET_ROOTFS_EXT4FS) += ext4
  65. fs-types-$(CONFIG_TARGET_ROOTFS_ISO) += iso
  66. fs-subtypes-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addsuffix -raw,$(addprefix jffs2-,$(JFFS2_BLOCKSIZE)))
  67. fs-subtypes-$(CONFIG_TARGET_ROOTFS_CPIOGZ) += cpiogz
  68. fs-subtypes-$(CONFIG_TARGET_ROOTFS_TARGZ) += targz
  69. TARGET_FILESYSTEMS := $(fs-types-y)
  70. FS_64K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-64k
  71. FS_128K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-128k
  72. FS_256K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-256k
  73. define add_jffs2_mark
  74. echo -ne '\xde\xad\xc0\xde' >> $(1)
  75. endef
  76. PROFILE_SANITIZED := $(call sanitize,$(PROFILE))
  77. define split_args
  78. $(foreach data, \
  79. $(subst |,$(space),\
  80. $(subst $(space),^,$(1))), \
  81. $(call $(2),$(strip $(subst ^,$(space),$(data)))))
  82. endef
  83. define build_cmd
  84. $(if $(Build/$(word 1,$(1))),,$(error Missing Build/$(word 1,$(1))))
  85. $(call Build/$(word 1,$(1)),$(wordlist 2,$(words $(1)),$(1)))
  86. endef
  87. define concat_cmd
  88. $(call split_args,$(1),build_cmd)
  89. endef
  90. # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
  91. define prepare_generic_squashfs
  92. $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
  93. endef
  94. define Image/BuildKernel/Initramfs
  95. $(call Image/Build/Initramfs)
  96. endef
  97. define Image/BuildKernel/MkuImage
  98. mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
  99. -n '$(call toupper,$(ARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $(4) $(5)
  100. endef
  101. define Image/BuildKernel/MkFIT
  102. $(TOPDIR)/scripts/mkits.sh \
  103. -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) $(if $(3),-d $(3)) -C $(4) -a $(5) -e $(6) \
  104. -A $(LINUX_KARCH) -v $(LINUX_VERSION)
  105. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
  106. endef
  107. # $(1) source dts file
  108. # $(2) target dtb file
  109. # $(3) extra CPP flags
  110. # $(4) extra DTC flags
  111. define Image/BuildDTB
  112. $(TARGET_CROSS)cpp -nostdinc -x assembler-with-cpp \
  113. -I$(DTS_DIR) \
  114. -I$(DTS_DIR)/include \
  115. -undef -D__DTS__ $(3) \
  116. -o $(2).tmp $(1)
  117. $(LINUX_DIR)/scripts/dtc/dtc -O dtb \
  118. -i$(dir $(1)) $(4) \
  119. -o $(2) $(2).tmp
  120. $(RM) $(2).tmp
  121. endef
  122. define Image/mkfs/jffs2/sub
  123. $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) --pad -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2) -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
  124. $(call add_jffs2_mark,$(KDIR)/root.jffs2-$(2))
  125. endef
  126. define Image/mkfs/jffs2/sub-raw
  127. $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2)-raw -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
  128. endef
  129. define Image/mkfs/jffs2/template
  130. Image/mkfs/jffs2-$(1) = $$(call Image/mkfs/jffs2/sub,$(1),$(1),$(JFFS2OPTS))
  131. Image/mkfs/jffs2-$(1)-raw = $$(call Image/mkfs/jffs2/sub-raw,$(1),$(1),$(JFFS2OPTS))
  132. endef
  133. define Image/mkfs/jffs2-nand/template
  134. Image/mkfs/jffs2-nand-$(1) = \
  135. $$(call Image/mkfs/jffs2/sub, \
  136. $(word 2,$(subst -, ,$(1))),nand-$(1), \
  137. $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst -, ,$(1))))
  138. endef
  139. $(eval $(foreach S,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/template,$(S))))
  140. $(eval $(foreach S,$(NAND_BLOCKSIZE),$(call Image/mkfs/jffs2-nand/template,$(S))))
  141. define Image/mkfs/squashfs
  142. $(STAGING_DIR_HOST)/bin/mksquashfs4 $(TARGET_DIR) $(KDIR)/root.squashfs -nopad -noappend -root-owned -comp $(SQUASHFSCOMP) $(SQUASHFSOPT) -processors $(if $(CONFIG_PKG_BUILD_JOBS),$(CONFIG_PKG_BUILD_JOBS),1) $(if $(SOURCE_DATE_EPOCH),-fixed-time $(SOURCE_DATE_EPOCH))
  143. endef
  144. # $(1): board name
  145. # $(2): rootfs type
  146. # $(3): kernel image
  147. ifneq ($(CONFIG_NAND_SUPPORT),)
  148. define Image/Build/SysupgradeNAND
  149. mkdir -p "$(KDIR_TMP)/sysupgrade-$(1)/"
  150. echo "BOARD=$(1)" > "$(KDIR_TMP)/sysupgrade-$(1)/CONTROL"
  151. [ -z "$(2)" ] || $(CP) "$(KDIR)/root.$(2)" "$(KDIR_TMP)/sysupgrade-$(1)/root"
  152. [ -z "$(3)" ] || $(CP) "$(3)" "$(KDIR_TMP)/sysupgrade-$(1)/kernel"
  153. (cd "$(KDIR_TMP)"; $(TAR) cvf \
  154. "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(2)-sysupgrade.tar" sysupgrade-$(1) \
  155. $(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
  156. )
  157. endef
  158. # $(1) board name
  159. # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
  160. # $(3) rootfstype (e.g. squashfs or ubifs)
  161. # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
  162. define Image/Build/UbinizeImage
  163. sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
  164. "$(KDIR)/root.$(3)" \
  165. "$(KDIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
  166. $(4)
  167. endef
  168. endif
  169. ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
  170. define Image/mkfs/ubifs/generate
  171. $(CP) ./ubinize$(1).cfg $(KDIR)
  172. ( cd $(KDIR); \
  173. $(STAGING_DIR_HOST)/bin/ubinize \
  174. $(if $($(PROFILE)_UBI_OPTS), \
  175. $(shell echo $($(PROFILE)_UBI_OPTS)), \
  176. $(shell echo $(UBI_OPTS)) \
  177. ) \
  178. -o $(KDIR)/root$(1).ubi \
  179. ubinize$(1).cfg \
  180. )
  181. endef
  182. define Image/mkfs/ubifs
  183. ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
  184. $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
  185. $(if $($(PROFILE)_UBIFS_OPTS), \
  186. $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
  187. $(shell echo $(UBIFS_OPTS)) \
  188. ) \
  189. $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
  190. $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
  191. $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
  192. $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
  193. $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
  194. --squash-uids \
  195. -o $(KDIR)/root.ubifs \
  196. -d $(TARGET_DIR)
  197. endif
  198. $(call Image/Build,ubifs)
  199. ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
  200. $(if $(wildcard ./ubinize.cfg),$(call Image/mkfs/ubifs/generate,))
  201. $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
  202. endif
  203. $(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi))
  204. endef
  205. endif
  206. define Image/mkfs/cpiogz
  207. ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9n >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
  208. endef
  209. define Image/mkfs/targz
  210. $(TAR) -cp --numeric-owner --owner=0 --group=0 --sort=name \
  211. $(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
  212. -C $(TARGET_DIR)/ . | gzip -9n > $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE_SANITIZED),-$(PROFILE_SANITIZED))-rootfs.tar.gz
  213. endef
  214. E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024)))
  215. define Image/mkfs/ext4
  216. $(STAGING_DIR_HOST)/bin/make_ext4fs \
  217. -l $(E2SIZE) -b $(CONFIG_TARGET_EXT4_BLOCKSIZE) \
  218. -i $(CONFIG_TARGET_EXT4_MAXINODE) \
  219. -m $(CONFIG_TARGET_EXT4_RESERVED_PCT) \
  220. $(if $(CONFIG_TARGET_EXT4_JOURNAL),,-J) \
  221. $(if $(SOURCE_DATE_EPOCH),-T $(SOURCE_DATE_EPOCH)) \
  222. $(KDIR)/root.ext4 $(TARGET_DIR)/
  223. endef
  224. define Image/mkfs/prepare/default
  225. # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
  226. - $(FIND) $(TARGET_DIR) -type f -not -perm /0100 -not -name 'ssh_host*' -not -name 'shadow' -print0 | $(XARGS) -0 chmod u+rw,g+r,o+r
  227. - $(FIND) $(TARGET_DIR) -type f -perm /0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
  228. - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
  229. $(INSTALL_DIR) $(TARGET_DIR)/tmp $(TARGET_DIR)/overlay
  230. chmod 1777 $(TARGET_DIR)/tmp
  231. endef
  232. define Image/mkfs/prepare
  233. $(call Image/mkfs/prepare/default)
  234. endef
  235. define Image/Checksum
  236. ( cd ${BIN_DIR} ; \
  237. $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs $1 > $2 \
  238. )
  239. endef
  240. define BuildImage/mkfs
  241. install: mkfs-$(1)
  242. .PHONY: mkfs-$(1)
  243. mkfs-$(1): kernel_prepare
  244. $(Image/mkfs/$(1))
  245. $(call Build/mkfs/default,$(1))
  246. $(call Build/mkfs/$(1),$(1))
  247. $(KDIR)/root.$(1): mkfs-$(1)
  248. endef
  249. # Build commands that can be called from Device/* templates
  250. define Build/uImage
  251. mkimage -A $(LINUX_KARCH) \
  252. -O linux -T kernel \
  253. -C $(1) -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  254. -n '$(call toupper,$(LINUX_KARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $@ $@.new
  255. @mv $@.new $@
  256. endef
  257. define Build/netgear-chk
  258. $(STAGING_DIR_HOST)/bin/mkchkimg \
  259. -o $@.new \
  260. -k $@ \
  261. -b $(NETGEAR_BOARD_ID) \
  262. -r $(NETGEAR_REGION)
  263. mv $@.new $@
  264. endef
  265. define Build/netgear-dni
  266. $(STAGING_DIR_HOST)/bin/mkdniimg \
  267. -B $(NETGEAR_BOARD_ID) -v OpenWrt.$(REVISION) \
  268. $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
  269. -r "$(1)" \
  270. -i $@ -o $@.new
  271. mv $@.new $@
  272. endef
  273. define Build/tplink-safeloader
  274. -$(STAGING_DIR_HOST)/bin/tplink-safeloader \
  275. -B $(TPLINK_BOARD_NAME) \
  276. -V $(REVISION) \
  277. -k $(word 1,$^) \
  278. -r $@ \
  279. -o $@.new \
  280. -j \
  281. $(wordlist 2,$(words $(1)),$(1)) \
  282. $(if $(findstring sysupgrade,$(word 1,$(1))),-S) && mv $@.new $@ || rm -f $@
  283. endef
  284. define Build/fit
  285. $(TOPDIR)/scripts/mkits.sh \
  286. -D $(DEVICE_NAME) -o $@.its -k $@ \
  287. $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
  288. -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  289. -A $(LINUX_KARCH) -v $(LINUX_VERSION)
  290. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
  291. @mv $@.new $@
  292. endef
  293. define Build/lzma
  294. $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
  295. endef
  296. define Build/lzma-no-dict
  297. $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
  298. @mv $@.new $@
  299. endef
  300. define Build/gzip
  301. gzip -9n -c $@ $(1) > $@.new
  302. @mv $@.new $@
  303. endef
  304. define Build/jffs2
  305. rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
  306. mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
  307. cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
  308. $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
  309. $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
  310. --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
  311. -o $@.new \
  312. -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
  313. 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
  314. $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
  315. -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
  316. @mv $@.new $@
  317. endef
  318. define Build/kernel-bin
  319. rm -f $@
  320. cp $< $@
  321. endef
  322. define Build/patch-cmdline
  323. $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
  324. endef
  325. define Build/append-kernel
  326. dd if=$(word 1,$^) $(if $(1),bs=$(1) conv=sync) >> $@
  327. endef
  328. define Build/append-rootfs
  329. dd if=$(word 2,$^) $(if $(1),bs=$(1) conv=sync) >> $@
  330. endef
  331. define Build/append-ubi
  332. sh $(TOPDIR)/scripts/ubinize-image.sh \
  333. $(if $(UBOOTENV_IN_UBI),--uboot-env) \
  334. $(if $(KERNEL_IN_UBI),--kernel $(word 1,$^)) \
  335. $(word 2,$^) \
  336. $@.tmp \
  337. -p $(BLOCKSIZE) -m $(PAGESIZE) -E 5 \
  338. $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE))
  339. cat $@.tmp >> $@
  340. rm $@.tmp
  341. endef
  342. define Build/pad-to
  343. dd if=$@ of=$@.new bs=$(1) conv=sync
  344. mv $@.new $@
  345. endef
  346. define Build/pad-rootfs
  347. $(call prepare_generic_squashfs,$@ $(1))
  348. endef
  349. define Build/pad-offset
  350. let \
  351. size="$$(stat -c%s $@)" \
  352. pad="$(word 1, $(1))" \
  353. offset="$(word 2, $(1))" \
  354. pad="(pad - ((size + offset) % pad)) % pad" \
  355. newsize='size + pad'; \
  356. dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
  357. mv $@.new $@
  358. endef
  359. define Build/check-size
  360. @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
  361. echo "WARNING: Image file $@ is too big" >&2; \
  362. rm -f $@; \
  363. }
  364. endef
  365. define Build/combined-image
  366. -sh $(TOPDIR)/scripts/combined-image.sh \
  367. "$(word 1,$^)" \
  368. "$@" \
  369. "$@.new"
  370. @mv $@.new $@
  371. endef
  372. define Build/sysupgrade-nand
  373. sh $(TOPDIR)/scripts/sysupgrade-nand.sh \
  374. --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
  375. --kernel $(word 1,$^) \
  376. --rootfs $(word 2,$^) \
  377. $@
  378. endef
  379. define Device/Init
  380. PROFILES := $(PROFILE)
  381. DEVICE_NAME := $(1)
  382. KERNEL:=
  383. KERNEL_INITRAMFS = $$(KERNEL)
  384. KERNEL_SIZE:=
  385. CMDLINE:=
  386. IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
  387. IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
  388. KERNEL_PREFIX = $(1)
  389. KERNEL_SUFFIX := -kernel.bin
  390. KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
  391. KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
  392. KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_SUFFIX)
  393. KERNEL_INITRAMFS_NAME = $$(KERNEL_NAME)-initramfs
  394. KERNEL_INSTALL :=
  395. KERNEL_NAME := vmlinux
  396. KERNEL_DEPENDS :=
  397. KERNEL_SIZE :=
  398. FILESYSTEMS := $(TARGET_FILESYSTEMS)
  399. endef
  400. define Device/ExportVar
  401. $(1) : $(2):=$$($(2))
  402. endef
  403. define Device/Export
  404. $(foreach var,$(DEVICE_VARS) DEVICE_NAME KERNEL KERNEL_INITRAMFS KERNEL_INITRAMFS_IMAGE,$(call Device/ExportVar,$(1),$(var)))
  405. $(1) : FILESYSTEM:=$(2)
  406. endef
  407. define Device/Check
  408. _TARGET = $$(if $$(filter $(PROFILE),$$(PROFILES)),install,install-disabled)
  409. _COMPILE_TARGET = $$(if $(if $(IB),,$(CONFIG_IB)$$(filter $(PROFILE),$$(PROFILES))),compile,compile-disabled)
  410. endef
  411. ifndef IB
  412. define Device/Build/initramfs
  413. $(call Device/Export,$(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE),$(1))
  414. $$(_TARGET): $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE)
  415. $(KDIR)/$$(KERNEL_INITRAMFS_NAME):: image_prepare
  416. $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE)
  417. cp $$^ $$@
  418. $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
  419. @rm -f $$@
  420. $$(call concat_cmd,$$(KERNEL_INITRAMFS))
  421. endef
  422. endif
  423. define Device/Build/check_size
  424. @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
  425. echo "WARNING: Image file $@ is too big" >&2; \
  426. rm -f $@; \
  427. }
  428. endef
  429. define Device/Build/compile
  430. $$(_COMPILE_TARGET): $(KDIR)/$(1)
  431. $(eval $(call Device/Export,$(KDIR)/$(1)))
  432. $(KDIR)/$(1):
  433. $$(call concat_cmd,$(COMPILE/$(1)))
  434. endef
  435. define Device/Build/kernel
  436. _KERNEL_IMAGES += $(KDIR)/$$(KERNEL_NAME)
  437. $(KDIR)/$$(KERNEL_NAME):: image_prepare
  438. $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
  439. $(call Device/Export,$(KDIR)/$$(KERNEL_IMAGE),$(1))
  440. $(BIN_DIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_IMAGE)
  441. cp $$^ $$@
  442. ifndef IB
  443. ifdef CONFIG_IB
  444. install: $(KDIR)/$$(KERNEL_IMAGE)
  445. endif
  446. $(KDIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
  447. @rm -f $$@
  448. $$(call concat_cmd,$$(KERNEL))
  449. $$(if $$(KERNEL_SIZE),$$(call Device/Build/check_size,$$(KERNEL_SIZE)))
  450. endif
  451. endef
  452. define Device/Build/image
  453. $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
  454. $(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
  455. $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$$(KERNEL_IMAGE) $(KDIR)/root.$(1)
  456. @rm -f $$@
  457. [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
  458. $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
  459. .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
  460. $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
  461. cp $$^ $$@
  462. endef
  463. define Device/Build
  464. $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
  465. $(call Device/Build/kernel,$(1))
  466. $$(eval $$(foreach compile,$$(COMPILE), \
  467. $$(call Device/Build/compile,$$(compile),$(1))))
  468. $$(eval $$(foreach image,$$(IMAGES), \
  469. $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
  470. $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
  471. endef
  472. define Device
  473. $(call Device/Init,$(1))
  474. $(call Device/Default,$(1))
  475. $(call Device/Check,$(1))
  476. $(call Device/$(1),$(1))
  477. $(call Device/Build,$(1))
  478. endef
  479. define BuildImage
  480. download:
  481. prepare:
  482. compile:
  483. clean:
  484. image_prepare:
  485. ifeq ($(IB),)
  486. .PHONY: download prepare compile clean image_prepare mkfs_prepare kernel_prepare install
  487. compile:
  488. $(call Build/Compile)
  489. clean:
  490. $(call Build/Clean)
  491. image_prepare: compile
  492. mkdir -p $(KDIR)/tmp
  493. $(call Image/Prepare)
  494. else
  495. image_prepare:
  496. mkdir -p $(KDIR)/tmp
  497. endif
  498. mkfs_prepare: image_prepare
  499. $(call Image/mkfs/prepare)
  500. kernel_prepare: mkfs_prepare
  501. $(call Image/BuildKernel)
  502. $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
  503. $(call Image/InstallKernel)
  504. $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
  505. $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
  506. install: kernel_prepare
  507. $(foreach fs,$(TARGET_FILESYSTEMS),
  508. $(call Image/Build,$(fs))
  509. )
  510. $(call Image/mkfs/ubifs)
  511. $(call Image/Checksum,md5sum --binary,md5sums)
  512. $(call Image/Checksum,openssl dgst -sha256,sha256sums)
  513. endef