image-commands.mk 11 KB

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