2
0

image-commands.mk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. # Build commands that can be called from Device/* templates
  2. IMAGE_KERNEL = $(word 1,$^)
  3. IMAGE_ROOTFS = $(word 2,$^)
  4. define rootfs_align
  5. $(patsubst %-256k,0x40000,$(patsubst %-128k,0x20000,$(patsubst %-64k,0x10000,$(patsubst squashfs%,0x4,$(patsubst root.%,%,$(1))))))
  6. endef
  7. define Build/append-dtb
  8. cat $(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb >> $@
  9. endef
  10. define Build/append-dtb-elf
  11. $(TARGET_CROSS)objcopy \
  12. --set-section-flags=.appended_dtb=alloc,contents \
  13. --update-section \
  14. .appended_dtb=$(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb $@
  15. endef
  16. define Build/append-kernel
  17. dd if=$(IMAGE_KERNEL) >> $@
  18. endef
  19. compat_version=$(if $(DEVICE_COMPAT_VERSION),$(DEVICE_COMPAT_VERSION),1.0)
  20. json_quote=$(subst ','\'',$(subst ",\",$(1)))
  21. #")')
  22. legacy_supported_message=$(SUPPORTED_DEVICES) - Image version mismatch: image $(compat_version), \
  23. device 1.0. Please wipe config during upgrade (force required) or reinstall. \
  24. $(if $(DEVICE_COMPAT_MESSAGE),Reason: $(DEVICE_COMPAT_MESSAGE),Please check documentation ...)
  25. metadata_devices=$(if $(1),$(subst "$(space)","$(comma)",$(strip $(foreach v,$(1),"$(call json_quote,$(v))"))))
  26. metadata_json = \
  27. '{ $(if $(IMAGE_METADATA),$(IMAGE_METADATA)$(comma)) \
  28. "metadata_version": "1.1", \
  29. "compat_version": "$(call json_quote,$(compat_version))", \
  30. $(if $(DEVICE_COMPAT_MESSAGE),"compat_message": "$(call json_quote,$(DEVICE_COMPAT_MESSAGE))"$(comma)) \
  31. $(if $(filter-out 1.0,$(compat_version)),"new_supported_devices": \
  32. [$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma) \
  33. "supported_devices": ["$(call json_quote,$(legacy_supported_message))"]$(comma)) \
  34. $(if $(filter 1.0,$(compat_version)),"supported_devices":[$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma)) \
  35. "version": { \
  36. "dist": "$(call json_quote,$(VERSION_DIST))", \
  37. "version": "$(call json_quote,$(VERSION_NUMBER))", \
  38. "revision": "$(call json_quote,$(REVISION))", \
  39. "target": "$(call json_quote,$(TARGETID))", \
  40. "board": "$(call json_quote,$(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)))" \
  41. } \
  42. }'
  43. define Build/append-metadata
  44. $(if $(SUPPORTED_DEVICES),-echo $(call metadata_json) | fwtool -I - $@)
  45. [ ! -s "$(BUILD_KEY)" -o ! -s "$(BUILD_KEY).ucert" -o ! -s "$@" ] || { \
  46. cp "$(BUILD_KEY).ucert" "[email protected]" ;\
  47. usign -S -m "$@" -s "$(BUILD_KEY)" -x "[email protected]" ;\
  48. ucert -A -c "[email protected]" -x "[email protected]" ;\
  49. fwtool -S "[email protected]" "$@" ;\
  50. }
  51. endef
  52. define Build/append-rootfs
  53. dd if=$(IMAGE_ROOTFS) >> $@
  54. endef
  55. define Build/append-squashfs-fakeroot-be
  56. rm -rf [email protected] [email protected]
  57. mkdir [email protected]
  58. $(STAGING_DIR_HOST)/bin/mksquashfs-lzma \
  59. [email protected] [email protected] \
  60. -noappend -root-owned -be -nopad -b 65536 \
  61. $(if $(SOURCE_DATE_EPOCH),-fixed-time $(SOURCE_DATE_EPOCH))
  62. cat [email protected] >> $@
  63. endef
  64. define Build/append-string
  65. echo -n $(1) >> $@
  66. endef
  67. define Build/append-ubi
  68. sh $(TOPDIR)/scripts/ubinize-image.sh \
  69. $(if $(UBOOTENV_IN_UBI),--uboot-env) \
  70. $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
  71. $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
  72. $(IMAGE_ROOTFS) \
  73. [email protected] \
  74. -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
  75. $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
  76. $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
  77. $(UBINIZE_OPTS)
  78. cat [email protected] >> $@
  79. rm [email protected]
  80. endef
  81. define Build/append-uboot
  82. dd if=$(UBOOT_PATH) >> $@
  83. endef
  84. # append a fake/empty uImage header, to fool bootloaders rootfs integrity check
  85. # for example
  86. define Build/append-uImage-fakehdr
  87. $(eval type=$(word 1,$(1)))
  88. $(eval magic=$(word 2,$(1)))
  89. touch [email protected]
  90. $(STAGING_DIR_HOST)/bin/mkimage \
  91. -A $(LINUX_KARCH) -O linux -T $(type) -C none \
  92. -n '$(VERSION_DIST) fake $(type)' \
  93. $(if $(magic),-M $(magic)) \
  94. -d [email protected] \
  95. -s \
  96. [email protected]
  97. cat [email protected] >> $@
  98. endef
  99. define Build/buffalo-dhp-image
  100. $(STAGING_DIR_HOST)/bin/mkdhpimg $@ [email protected]
  101. mv [email protected] $@
  102. endef
  103. define Build/buffalo-enc
  104. $(eval product=$(word 1,$(1)))
  105. $(eval version=$(word 2,$(1)))
  106. $(eval args=$(wordlist 3,$(words $(1)),$(1)))
  107. $(STAGING_DIR_HOST)/bin/buffalo-enc \
  108. -p $(product) -v $(version) $(args) \
  109. -i $@ -o [email protected]
  110. mv [email protected] $@
  111. endef
  112. define Build/buffalo-enc-tag
  113. $(call Build/buffalo-enc,'' '' -S 152 $(1))
  114. endef
  115. define Build/buffalo-tag-dhp
  116. $(eval product=$(word 1,$(1)))
  117. $(eval region=$(word 2,$(1)))
  118. $(eval language=$(word 3,$(1)))
  119. $(STAGING_DIR_HOST)/bin/buffalo-tag \
  120. -d 0x01000000 -w 1 \
  121. -a $(BUFFALO_TAG_PLATFORM) \
  122. -v $(BUFFALO_TAG_VERSION) -m $(BUFFALO_TAG_MINOR) \
  123. -b $(product) -p $(product) \
  124. -r $(region) -r $(region) -l $(language) \
  125. -I $@ -o [email protected]
  126. mv [email protected] $@
  127. endef
  128. define Build/check-size
  129. @imagesize="$$(stat -c%s $@)"; \
  130. limitsize="$$(($(subst k,* 1024,$(subst m, * 1024k,$(if $(1),$(1),$(IMAGE_SIZE))))))"; \
  131. [ $$limitsize -ge $$imagesize ] || { \
  132. echo "WARNING: Image file $@ is too big: $$imagesize > $$limitsize" >&2; \
  133. rm -f $@; \
  134. }
  135. endef
  136. define Build/eva-image
  137. $(STAGING_DIR_HOST)/bin/lzma2eva $(KERNEL_LOADADDR) $(KERNEL_LOADADDR) $@ [email protected]
  138. mv [email protected] $@
  139. endef
  140. define Build/fit
  141. $(TOPDIR)/scripts/mkits.sh \
  142. -D $(DEVICE_NAME) -o [email protected] -k $@ \
  143. $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
  144. -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  145. $(if $(DEVICE_FDT_NUM),-n $(DEVICE_FDT_NUM)) \
  146. -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config@1") \
  147. -A $(LINUX_KARCH) -v $(LINUX_VERSION)
  148. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f [email protected] [email protected]
  149. @mv [email protected] $@
  150. endef
  151. define Build/gzip
  152. gzip -f -9n -c $@ $(1) > [email protected]
  153. @mv [email protected] $@
  154. endef
  155. define Build/install-dtb
  156. $(call locked, \
  157. $(foreach dts,$(DEVICE_DTS), \
  158. $(CP) \
  159. $(DTS_DIR)/$(dts).dtb \
  160. $(BIN_DIR)/$(IMG_PREFIX)-$(dts).dtb; \
  161. ), \
  162. install-dtb-$(IMG_PREFIX) \
  163. )
  164. endef
  165. define Build/jffs2
  166. rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
  167. mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
  168. cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
  169. $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
  170. $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
  171. --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
  172. -o [email protected] \
  173. -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
  174. 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
  175. $(STAGING_DIR_HOST)/bin/padjffs2 [email protected] -J $(patsubst %k,,$(BLOCKSIZE))
  176. -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
  177. @mv [email protected] $@
  178. endef
  179. define Build/kernel2minor
  180. kernel2minor -k $@ -r [email protected] $(1)
  181. mv [email protected] $@
  182. endef
  183. define Build/kernel-bin
  184. rm -f $@
  185. cp $< $@
  186. endef
  187. define Build/linksys-image
  188. $(TOPDIR)/scripts/linksys-image.sh \
  189. "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \
  190. $@ [email protected]
  191. mv [email protected] $@
  192. endef
  193. define Build/lzma
  194. $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
  195. endef
  196. define Build/lzma-no-dict
  197. $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) [email protected]
  198. @mv [email protected] $@
  199. endef
  200. define Build/netgear-chk
  201. $(STAGING_DIR_HOST)/bin/mkchkimg \
  202. -o [email protected] \
  203. -k $@ \
  204. -b $(NETGEAR_BOARD_ID) \
  205. $(if $(NETGEAR_REGION),-r $(NETGEAR_REGION),)
  206. mv [email protected] $@
  207. endef
  208. define Build/netgear-dni
  209. $(STAGING_DIR_HOST)/bin/mkdniimg \
  210. -B $(NETGEAR_BOARD_ID) -v $(VERSION_DIST).$(firstword $(subst -, ,$(REVISION))) \
  211. $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
  212. -r "$(1)" \
  213. -i $@ -o [email protected]
  214. mv [email protected] $@
  215. endef
  216. define Build/openmesh-image
  217. $(TOPDIR)/scripts/om-fwupgradecfg-gen.sh \
  218. "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" \
  219. "[email protected]" \
  220. "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" \
  221. "$(call param_get_default,rootfs,$(1),$@)"
  222. $(TOPDIR)/scripts/combined-ext-image.sh \
  223. "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" "$@" \
  224. "[email protected]" "fwupgrade.cfg" \
  225. "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" "kernel" \
  226. "$(call param_get_default,rootfs,$(1),$@)" "rootfs"
  227. endef
  228. define Build/pad-extra
  229. dd if=/dev/zero bs=$(1) count=1 >> $@
  230. endef
  231. define Build/pad-offset
  232. let \
  233. size="$$(stat -c%s $@)" \
  234. pad="$(subst k,* 1024,$(word 1, $(1)))" \
  235. offset="$(subst k,* 1024,$(word 2, $(1)))" \
  236. pad="(pad - ((size + offset) % pad)) % pad" \
  237. newsize='size + pad'; \
  238. dd if=$@ [email protected] bs=$$newsize count=1 conv=sync
  239. mv [email protected] $@
  240. endef
  241. define Build/pad-rootfs
  242. $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) \
  243. $(if $(BLOCKSIZE),$(BLOCKSIZE:%k=%),4 8 16 64 128 256)
  244. endef
  245. define Build/pad-to
  246. $(call Image/pad-to,$@,$(1))
  247. endef
  248. define Build/patch-cmdline
  249. $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
  250. endef
  251. # Convert a raw image into a $1 type image.
  252. # E.g. | qemu-image vdi
  253. define Build/qemu-image
  254. qemu-img convert -f raw -O $1 $@ [email protected]
  255. @mv [email protected] $@
  256. endef
  257. define Build/qsdk-ipq-factory-nand
  258. $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
  259. [email protected] ubi $@
  260. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f [email protected] [email protected]
  261. @mv [email protected] $@
  262. endef
  263. define Build/qsdk-ipq-factory-nor
  264. $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
  265. [email protected] hlos $(IMAGE_KERNEL) rootfs $(IMAGE_ROOTFS)
  266. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f [email protected] [email protected]
  267. @mv [email protected] $@
  268. endef
  269. define Build/seama
  270. $(STAGING_DIR_HOST)/bin/seama -i $@ \
  271. -m "dev=/dev/mtdblock/$(SEAMA_MTDBLOCK)" -m "type=firmware"
  272. mv [email protected] $@
  273. endef
  274. define Build/seama-seal
  275. $(STAGING_DIR_HOST)/bin/seama -i $@ -s [email protected] \
  276. -m "signature=$(SEAMA_SIGNATURE)"
  277. mv [email protected] $@
  278. endef
  279. define Build/senao-header
  280. $(STAGING_DIR_HOST)/bin/mksenaofw $(1) -e $@ -o [email protected]
  281. mv [email protected] $@
  282. endef
  283. define Build/sysupgrade-tar
  284. sh $(TOPDIR)/scripts/sysupgrade-tar.sh \
  285. --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
  286. --kernel $(call param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
  287. --rootfs $(call param_get_default,rootfs,$(1),$(IMAGE_ROOTFS)) \
  288. $@
  289. endef
  290. define Build/tplink-safeloader
  291. -$(STAGING_DIR_HOST)/bin/tplink-safeloader \
  292. -B $(TPLINK_BOARD_ID) \
  293. -V $(REVISION) \
  294. -k $(IMAGE_KERNEL) \
  295. -r $@ \
  296. -o [email protected] \
  297. -j \
  298. $(wordlist 2,$(words $(1)),$(1)) \
  299. $(if $(findstring sysupgrade,$(word 1,$(1))),-S) && mv [email protected] $@ || rm -f $@
  300. endef
  301. define Build/tplink-v1-header
  302. $(STAGING_DIR_HOST)/bin/mktplinkfw \
  303. -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
  304. -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  305. -m $(TPLINK_HEADER_VERSION) -N "$(VERSION_DIST)" -V $(REVISION) \
  306. -k $@ -o [email protected] $(1)
  307. @mv [email protected] $@
  308. endef
  309. # combine kernel and rootfs into one image
  310. # mktplinkfw <type> <optional extra arguments to mktplinkfw binary>
  311. # <type> is "sysupgrade" or "factory"
  312. #
  313. # -a align the rootfs start on an <align> bytes boundary
  314. # -j add jffs2 end-of-filesystem markers
  315. # -s strip padding from end of the image
  316. # -X reserve <size> bytes in the firmware image (hexval prefixed with 0x)
  317. define Build/tplink-v1-image
  318. -$(STAGING_DIR_HOST)/bin/mktplinkfw \
  319. -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -F $(TPLINK_FLASHLAYOUT) \
  320. -N "$(VERSION_DIST)" -V $(REVISION) -m $(TPLINK_HEADER_VERSION) \
  321. -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o [email protected] -j -X 0x40000 \
  322. -a $(call rootfs_align,$(FILESYSTEM)) \
  323. $(wordlist 2,$(words $(1)),$(1)) \
  324. $(if $(findstring sysupgrade,$(word 1,$(1))),-s) && mv [email protected] $@ || rm -f $@
  325. endef
  326. define Build/tplink-v2-header
  327. $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
  328. -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
  329. -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  330. -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
  331. -T $(TPLINK_HVERSION) -V "ver. 2.0" \
  332. -k $@ -o [email protected] $(1)
  333. @mv [email protected] $@
  334. endef
  335. define Build/tplink-v2-image
  336. $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
  337. -H $(TPLINK_HWID) -W $(TPLINK_HWREV) \
  338. -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
  339. -T $(TPLINK_HVERSION) -V "ver. 2.0" -a 0x4 -j \
  340. -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o [email protected] $(1)
  341. cat [email protected] >> $@
  342. rm -rf [email protected]
  343. endef
  344. define Build/uImage
  345. mkimage \
  346. -A $(LINUX_KARCH) \
  347. -O linux \
  348. -T kernel \
  349. -C $(word 1,$(1)) \
  350. -a $(KERNEL_LOADADDR) \
  351. -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  352. -n '$(if $(UIMAGE_NAME),$(UIMAGE_NAME),$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION))' \
  353. $(if $(UIMAGE_MAGIC),-M $(UIMAGE_MAGIC)) \
  354. $(wordlist 2,$(words $(1)),$(1)) \
  355. -d $@ [email protected]
  356. mv [email protected] $@
  357. endef
  358. define Build/xor-image
  359. $(STAGING_DIR_HOST)/bin/xorimage -i $@ -o [email protected] $(1)
  360. mv [email protected] $@
  361. endef
  362. define Build/zip
  363. mkdir [email protected]
  364. mv $@ [email protected]/$(1)
  365. zip -j -X \
  366. $(if $(SOURCE_DATE_EPOCH),--mtime="$(SOURCE_DATE_EPOCH)") \
  367. $@ [email protected]/$(if $(1),$(1),$@)
  368. rm -rf [email protected]
  369. endef
  370. define Build/zyxel-ras-image
  371. let \
  372. newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
  373. $(STAGING_DIR_HOST)/bin/mkrasimage \
  374. -b $(RAS_BOARD) \
  375. -v $(RAS_VERSION) \
  376. -r $@ \
  377. -s $$newsize \
  378. -o [email protected] \
  379. $(if $(findstring separate-kernel,$(word 1,$(1))),-k $(IMAGE_KERNEL)) \
  380. && mv [email protected] $@
  381. endef