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