image-commands.mk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. $(if $(DEVICE_FDT_NUM),-n $(DEVICE_FDT_NUM)) \
  142. -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config@1") \
  143. -A $(LINUX_KARCH) -v $(LINUX_VERSION)
  144. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f [email protected] [email protected]
  145. @mv [email protected] $@
  146. endef
  147. define Build/lzma
  148. $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
  149. endef
  150. define Build/lzma-no-dict
  151. $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) [email protected]
  152. @mv [email protected] $@
  153. endef
  154. define Build/gzip
  155. gzip -f -9n -c $@ $(1) > [email protected]
  156. @mv [email protected] $@
  157. endef
  158. define Build/zip
  159. mkdir [email protected]
  160. mv $@ [email protected]/$(1)
  161. zip -j -X \
  162. $(if $(SOURCE_DATE_EPOCH),--mtime="$(SOURCE_DATE_EPOCH)") \
  163. $@ [email protected]/$(if $(1),$(1),$@)
  164. rm -rf [email protected]
  165. endef
  166. define Build/jffs2
  167. rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
  168. mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
  169. cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
  170. $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
  171. $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
  172. --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
  173. -o [email protected] \
  174. -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
  175. 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
  176. $(STAGING_DIR_HOST)/bin/padjffs2 [email protected] -J $(patsubst %k,,$(BLOCKSIZE))
  177. -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
  178. @mv [email protected] $@
  179. endef
  180. define Build/kernel-bin
  181. rm -f $@
  182. cp $< $@
  183. endef
  184. define Build/patch-cmdline
  185. $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
  186. endef
  187. define Build/append-kernel
  188. dd if=$(IMAGE_KERNEL) >> $@
  189. endef
  190. define Build/append-rootfs
  191. dd if=$(IMAGE_ROOTFS) >> $@
  192. endef
  193. define Build/append-ubi
  194. sh $(TOPDIR)/scripts/ubinize-image.sh \
  195. $(if $(UBOOTENV_IN_UBI),--uboot-env) \
  196. $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
  197. $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
  198. $(IMAGE_ROOTFS) \
  199. [email protected] \
  200. -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
  201. $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
  202. $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
  203. $(UBINIZE_OPTS)
  204. cat [email protected] >> $@
  205. rm [email protected]
  206. endef
  207. define Build/append-uboot
  208. dd if=$(UBOOT_PATH) >> $@
  209. endef
  210. define Build/pad-to
  211. $(call Image/pad-to,$@,$(1))
  212. endef
  213. define Build/pad-extra
  214. dd if=/dev/zero bs=$(1) count=1 >> $@
  215. endef
  216. define Build/pad-rootfs
  217. $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) \
  218. $(if $(BLOCKSIZE),$(BLOCKSIZE:%k=%),4 8 16 64 128 256)
  219. endef
  220. define Build/pad-offset
  221. let \
  222. size="$$(stat -c%s $@)" \
  223. pad="$(subst k,* 1024,$(word 1, $(1)))" \
  224. offset="$(subst k,* 1024,$(word 2, $(1)))" \
  225. pad="(pad - ((size + offset) % pad)) % pad" \
  226. newsize='size + pad'; \
  227. dd if=$@ [email protected] bs=$$newsize count=1 conv=sync
  228. mv [email protected] $@
  229. endef
  230. define Build/xor-image
  231. $(STAGING_DIR_HOST)/bin/xorimage -i $@ -o [email protected] $(1)
  232. mv [email protected] $@
  233. endef
  234. define Build/check-size
  235. @imagesize="$$(stat -c%s $@)"; \
  236. limitsize="$$(($(subst k,* 1024,$(subst m, * 1024k,$(if $(1),$(1),$(IMAGE_SIZE))))))"; \
  237. [ $$limitsize -ge $$imagesize ] || { \
  238. echo "WARNING: Image file $@ is too big: $$imagesize > $$limitsize" >&2; \
  239. rm -f $@; \
  240. }
  241. endef
  242. define Build/combined-image
  243. -sh $(TOPDIR)/scripts/combined-image.sh \
  244. "$(IMAGE_KERNEL)" \
  245. "$@" \
  246. "[email protected]"
  247. @mv [email protected] $@
  248. endef
  249. define Build/linksys-image
  250. $(TOPDIR)/scripts/linksys-image.sh \
  251. "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \
  252. $@ [email protected]
  253. mv [email protected] $@
  254. endef
  255. define Build/openmesh-image
  256. $(TOPDIR)/scripts/om-fwupgradecfg-gen.sh \
  257. "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" \
  258. "[email protected]" \
  259. "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" \
  260. "$(call param_get_default,rootfs,$(1),$@)"
  261. $(TOPDIR)/scripts/combined-ext-image.sh \
  262. "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" "$@" \
  263. "[email protected]" "fwupgrade.cfg" \
  264. "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" "kernel" \
  265. "$(call param_get_default,rootfs,$(1),$@)" "rootfs"
  266. endef
  267. define Build/qsdk-ipq-factory-nand
  268. $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
  269. [email protected] ubi $@
  270. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f [email protected] [email protected]
  271. @mv [email protected] $@
  272. endef
  273. define Build/qsdk-ipq-factory-nor
  274. $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
  275. [email protected] hlos $(IMAGE_KERNEL) rootfs $(IMAGE_ROOTFS)
  276. PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f [email protected] [email protected]
  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-v1-header
  291. $(STAGING_DIR_HOST)/bin/mktplinkfw \
  292. -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
  293. -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  294. -m $(TPLINK_HEADER_VERSION) -N "$(VERSION_DIST)" -V $(REVISION) \
  295. -k $@ -o [email protected] $(1)
  296. @mv [email protected] $@
  297. endef
  298. # combine kernel and rootfs into one image
  299. # mktplinkfw <type> <optional extra arguments to mktplinkfw binary>
  300. # <type> is "sysupgrade" or "factory"
  301. #
  302. # -a align the rootfs start on an <align> bytes boundary
  303. # -j add jffs2 end-of-filesystem markers
  304. # -s strip padding from end of the image
  305. # -X reserve <size> bytes in the firmware image (hexval prefixed with 0x)
  306. define Build/tplink-v1-image
  307. -$(STAGING_DIR_HOST)/bin/mktplinkfw \
  308. -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -F $(TPLINK_FLASHLAYOUT) \
  309. -N "$(VERSION_DIST)" -V $(REVISION) -m $(TPLINK_HEADER_VERSION) \
  310. -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o [email protected] -j -X 0x40000 \
  311. -a $(call rootfs_align,$(FILESYSTEM)) \
  312. $(wordlist 2,$(words $(1)),$(1)) \
  313. $(if $(findstring sysupgrade,$(word 1,$(1))),-s) && mv [email protected] $@ || rm -f $@
  314. endef
  315. define Build/tplink-v2-header
  316. $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
  317. -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
  318. -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
  319. -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
  320. -T $(TPLINK_HVERSION) -V "ver. 2.0" \
  321. -k $@ -o [email protected] $(1)
  322. @mv [email protected] $@
  323. endef
  324. define Build/tplink-v2-image
  325. $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
  326. -H $(TPLINK_HWID) -W $(TPLINK_HWREV) \
  327. -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
  328. -T $(TPLINK_HVERSION) -V "ver. 2.0" -a 0x4 -j \
  329. -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o [email protected] $(1)
  330. cat [email protected] >> $@
  331. rm -rf [email protected]
  332. endef
  333. compat_version=$(if $(DEVICE_COMPAT_VERSION),$(DEVICE_COMPAT_VERSION),1.0)
  334. json_quote=$(subst ','\'',$(subst ",\",$(1)))
  335. #")')
  336. legacy_supported_message=$(SUPPORTED_DEVICES) - Image version mismatch: image $(compat_version), \
  337. device 1.0. Please wipe config during upgrade (force required) or reinstall. \
  338. $(if $(DEVICE_COMPAT_MESSAGE),Reason: $(DEVICE_COMPAT_MESSAGE),Please check documentation ...)
  339. metadata_devices=$(if $(1),$(subst "$(space)","$(comma)",$(strip $(foreach v,$(1),"$(call json_quote,$(v))"))))
  340. metadata_json = \
  341. '{ $(if $(IMAGE_METADATA),$(IMAGE_METADATA)$(comma)) \
  342. "metadata_version": "1.1", \
  343. "compat_version": "$(call json_quote,$(compat_version))", \
  344. $(if $(DEVICE_COMPAT_MESSAGE),"compat_message": "$(call json_quote,$(DEVICE_COMPAT_MESSAGE))"$(comma)) \
  345. $(if $(filter-out 1.0,$(compat_version)),"new_supported_devices": \
  346. [$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma) \
  347. "supported_devices": ["$(call json_quote,$(legacy_supported_message))"]$(comma)) \
  348. $(if $(filter 1.0,$(compat_version)),"supported_devices":[$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma)) \
  349. "version": { \
  350. "dist": "$(call json_quote,$(VERSION_DIST))", \
  351. "version": "$(call json_quote,$(VERSION_NUMBER))", \
  352. "revision": "$(call json_quote,$(REVISION))", \
  353. "target": "$(call json_quote,$(TARGETID))", \
  354. "board": "$(call json_quote,$(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)))" \
  355. } \
  356. }'
  357. define Build/append-metadata
  358. $(if $(SUPPORTED_DEVICES),-echo $(call metadata_json) | fwtool -I - $@)
  359. [ ! -s "$(BUILD_KEY)" -o ! -s "$(BUILD_KEY).ucert" -o ! -s "$@" ] || { \
  360. cp "$(BUILD_KEY).ucert" "[email protected]" ;\
  361. usign -S -m "$@" -s "$(BUILD_KEY)" -x "[email protected]" ;\
  362. ucert -A -c "[email protected]" -x "[email protected]" ;\
  363. fwtool -S "[email protected]" "$@" ;\
  364. }
  365. endef
  366. define Build/kernel2minor
  367. kernel2minor -k $@ -r [email protected] $(1)
  368. mv [email protected] $@
  369. endef
  370. # Convert a raw image into a $1 type image.
  371. # E.g. | qemu-image vdi
  372. define Build/qemu-image
  373. qemu-img convert -f raw -O $1 $@ [email protected]
  374. @mv [email protected] $@
  375. endef