image-commands.mk 10 KB

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