0107-procd-remove-procd-nand-package.patch 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Fri, 21 Apr 2017 20:37:58 +0200
  3. Subject: procd: remove procd-nand package
  4. We always want to support staged upgrades now, so it's better to include
  5. upgraded into the main package. /lib/upgrade/nand.sh is moved to
  6. base-files.
  7. The procd-nand-firstboot package is removed for now, it may return later
  8. as a separate package.
  9. Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
  10. diff --git a/package/base-files/Makefile b/package/base-files/Makefile
  11. index 9f6810f4d8bad89eb11db1023693ad59f7369d57..3e27e43c92af01b6cac212e77cde3d1db916ba6f 100644
  12. --- a/package/base-files/Makefile
  13. +++ b/package/base-files/Makefile
  14. @@ -31,7 +31,7 @@ endif
  15. define Package/base-files
  16. SECTION:=base
  17. CATEGORY:=Base system
  18. - DEPENDS:=+netifd +libc +procd +jsonfilter +SIGNED_PACKAGES:usign
  19. + DEPENDS:=+netifd +libc +procd +jsonfilter +SIGNED_PACKAGES:usign +NAND_SUPPORT:ubi-utils
  20. TITLE:=Base filesystem for OpenWrt
  21. URL:=http://openwrt.org/
  22. VERSION:=$(PKG_RELEASE)-$(REVISION)
  23. @@ -103,9 +103,16 @@ ifdef CONFIG_SIGNED_PACKAGES
  24. endef
  25. endif
  26. +ifeq ($(CONFIG_NAND_SUPPORT),)
  27. + define Package/base-files/nand-support
  28. + rm -f $(1)/lib/upgrade/nand.sh
  29. + endef
  30. +endif
  31. +
  32. define Package/base-files/install
  33. $(CP) ./files/* $(1)/
  34. $(Package/base-files/install-key)
  35. + $(Package/base-files/nand-support)
  36. if [ -d $(GENERIC_PLATFORM_DIR)/base-files/. ]; then \
  37. $(CP) $(GENERIC_PLATFORM_DIR)/base-files/* $(1)/; \
  38. fi
  39. diff --git a/package/base-files/files/lib/upgrade/nand.sh b/package/base-files/files/lib/upgrade/nand.sh
  40. new file mode 100644
  41. index 0000000000000000000000000000000000000000..ec2a014a00461ca897d521e2d065f5399f1f8c48
  42. --- /dev/null
  43. +++ b/package/base-files/files/lib/upgrade/nand.sh
  44. @@ -0,0 +1,365 @@
  45. +#!/bin/sh
  46. +# Copyright (C) 2014 OpenWrt.org
  47. +#
  48. +
  49. +. /lib/functions.sh
  50. +
  51. +# 'kernel' partition on NAND contains the kernel
  52. +CI_KERNPART="kernel"
  53. +
  54. +# 'ubi' partition on NAND contains UBI
  55. +CI_UBIPART="ubi"
  56. +
  57. +ubi_mknod() {
  58. + local dir="$1"
  59. + local dev="/dev/$(basename $dir)"
  60. +
  61. + [ -e "$dev" ] && return 0
  62. +
  63. + local devid="$(cat $dir/dev)"
  64. + local major="${devid%%:*}"
  65. + local minor="${devid##*:}"
  66. + mknod "$dev" c $major $minor
  67. +}
  68. +
  69. +nand_find_volume() {
  70. + local ubidevdir ubivoldir
  71. + ubidevdir="/sys/devices/virtual/ubi/$1"
  72. + [ ! -d "$ubidevdir" ] && return 1
  73. + for ubivoldir in $ubidevdir/${1}_*; do
  74. + [ ! -d "$ubivoldir" ] && continue
  75. + if [ "$( cat $ubivoldir/name )" = "$2" ]; then
  76. + basename $ubivoldir
  77. + ubi_mknod "$ubivoldir"
  78. + return 0
  79. + fi
  80. + done
  81. +}
  82. +
  83. +nand_find_ubi() {
  84. + local ubidevdir ubidev mtdnum
  85. + mtdnum="$( find_mtd_index $1 )"
  86. + [ ! "$mtdnum" ] && return 1
  87. + for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
  88. + [ ! -d "$ubidevdir" ] && continue
  89. + cmtdnum="$( cat $ubidevdir/mtd_num )"
  90. + [ ! "$mtdnum" ] && continue
  91. + if [ "$mtdnum" = "$cmtdnum" ]; then
  92. + ubidev=$( basename $ubidevdir )
  93. + ubi_mknod "$ubidevdir"
  94. + echo $ubidev
  95. + return 0
  96. + fi
  97. + done
  98. +}
  99. +
  100. +nand_get_magic_long() {
  101. + dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  102. +}
  103. +
  104. +get_magic_long_tar() {
  105. + ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
  106. +}
  107. +
  108. +identify_magic() {
  109. + local magic=$1
  110. + case "$magic" in
  111. + "55424923")
  112. + echo "ubi"
  113. + ;;
  114. + "31181006")
  115. + echo "ubifs"
  116. + ;;
  117. + "68737173")
  118. + echo "squashfs"
  119. + ;;
  120. + "d00dfeed")
  121. + echo "fit"
  122. + ;;
  123. + "4349"*)
  124. + echo "combined"
  125. + ;;
  126. + *)
  127. + echo "unknown $magic"
  128. + ;;
  129. + esac
  130. +}
  131. +
  132. +
  133. +identify() {
  134. + identify_magic $(nand_get_magic_long "$1" "${2:-0}")
  135. +}
  136. +
  137. +identify_tar() {
  138. + identify_magic $(get_magic_long_tar "$1" "$2")
  139. +}
  140. +
  141. +nand_restore_config() {
  142. + sync
  143. + local ubidev=$( nand_find_ubi $CI_UBIPART )
  144. + local ubivol="$( nand_find_volume $ubidev rootfs_data )"
  145. + [ ! "$ubivol" ] &&
  146. + ubivol="$( nand_find_volume $ubidev rootfs )"
  147. + mkdir /tmp/new_root
  148. + if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
  149. + echo "mounting ubifs $ubivol failed"
  150. + rmdir /tmp/new_root
  151. + return 1
  152. + fi
  153. + mv "$1" "/tmp/new_root/sysupgrade.tgz"
  154. + umount /tmp/new_root
  155. + sync
  156. + rmdir /tmp/new_root
  157. +}
  158. +
  159. +nand_upgrade_prepare_ubi() {
  160. + local rootfs_length="$1"
  161. + local rootfs_type="$2"
  162. + local has_kernel="${3:-0}"
  163. + local has_env="${4:-0}"
  164. +
  165. + local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
  166. + if [ ! "$mtdnum" ]; then
  167. + echo "cannot find ubi mtd partition $CI_UBIPART"
  168. + return 1
  169. + fi
  170. +
  171. + local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  172. + if [ ! "$ubidev" ]; then
  173. + ubiattach -m "$mtdnum"
  174. + sync
  175. + ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  176. + fi
  177. +
  178. + if [ ! "$ubidev" ]; then
  179. + ubiformat /dev/mtd$mtdnum -y
  180. + ubiattach -m "$mtdnum"
  181. + sync
  182. + ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  183. + [ "$has_env" -gt 0 ] && {
  184. + ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
  185. + ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
  186. + }
  187. + fi
  188. +
  189. + local kern_ubivol="$( nand_find_volume $ubidev kernel )"
  190. + local root_ubivol="$( nand_find_volume $ubidev rootfs )"
  191. + local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
  192. +
  193. + # remove ubiblock device of rootfs
  194. + local root_ubiblk="ubiblock${root_ubivol:3}"
  195. + if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
  196. + echo "removing $root_ubiblk"
  197. + if ! ubiblock -r /dev/$root_ubivol; then
  198. + echo "cannot remove $root_ubiblk"
  199. + return 1;
  200. + fi
  201. + fi
  202. +
  203. + # kill volumes
  204. + [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
  205. + [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
  206. + [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
  207. +
  208. + # update kernel
  209. + if [ "$has_kernel" = "1" ]; then
  210. + if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
  211. + echo "cannot create kernel volume"
  212. + return 1;
  213. + fi
  214. + fi
  215. +
  216. + # update rootfs
  217. + local root_size_param
  218. + if [ "$rootfs_type" = "ubifs" ]; then
  219. + root_size_param="-m"
  220. + else
  221. + root_size_param="-s $rootfs_length"
  222. + fi
  223. + if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
  224. + echo "cannot create rootfs volume"
  225. + return 1;
  226. + fi
  227. +
  228. + # create rootfs_data for non-ubifs rootfs
  229. + if [ "$rootfs_type" != "ubifs" ]; then
  230. + if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
  231. + echo "cannot initialize rootfs_data volume"
  232. + return 1
  233. + fi
  234. + fi
  235. + sync
  236. + return 0
  237. +}
  238. +
  239. +nand_do_upgrade_success() {
  240. + local conf_tar="/tmp/sysupgrade.tgz"
  241. +
  242. + sync
  243. + [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
  244. + echo "sysupgrade successful"
  245. + reboot -f
  246. +}
  247. +
  248. +# Flash the UBI image to MTD partition
  249. +nand_upgrade_ubinized() {
  250. + local ubi_file="$1"
  251. + local mtdnum="$(find_mtd_index "$CI_UBIPART")"
  252. +
  253. + [ ! "$mtdnum" ] && {
  254. + CI_UBIPART="rootfs"
  255. + mtdnum="$(find_mtd_index "$CI_UBIPART")"
  256. + }
  257. +
  258. + if [ ! "$mtdnum" ]; then
  259. + echo "cannot find mtd device $CI_UBIPART"
  260. + reboot -f
  261. + fi
  262. +
  263. + local mtddev="/dev/mtd${mtdnum}"
  264. + ubidetach -p "${mtddev}" || true
  265. + sync
  266. + ubiformat "${mtddev}" -y -f "${ubi_file}"
  267. + ubiattach -p "${mtddev}"
  268. + nand_do_upgrade_success
  269. +}
  270. +
  271. +# Write the UBIFS image to UBI volume
  272. +nand_upgrade_ubifs() {
  273. + local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
  274. +
  275. + nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
  276. +
  277. + local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  278. + local root_ubivol="$(nand_find_volume $ubidev rootfs)"
  279. + ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
  280. +
  281. + nand_do_upgrade_success
  282. +}
  283. +
  284. +nand_upgrade_tar() {
  285. + local tar_file="$1"
  286. + local board_name="$(cat /tmp/sysinfo/board_name)"
  287. + local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
  288. +
  289. + local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
  290. + local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
  291. +
  292. + local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)"
  293. +
  294. + local has_kernel=1
  295. + local has_env=0
  296. +
  297. + [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
  298. + tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - $CI_KERNPART
  299. + }
  300. + [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
  301. +
  302. + nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
  303. +
  304. + local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  305. + [ "$has_kernel" = "1" ] && {
  306. + local kern_ubivol="$(nand_find_volume $ubidev kernel)"
  307. + tar xf $tar_file sysupgrade-$board_name/kernel -O | \
  308. + ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
  309. + }
  310. +
  311. + local root_ubivol="$(nand_find_volume $ubidev rootfs)"
  312. + tar xf $tar_file sysupgrade-$board_name/root -O | \
  313. + ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
  314. +
  315. + nand_do_upgrade_success
  316. +}
  317. +
  318. +# Recognize type of passed file and start the upgrade process
  319. +nand_do_upgrade_stage2() {
  320. + local file_type=$(identify $1)
  321. +
  322. + [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
  323. +
  324. + case "$file_type" in
  325. + "ubi") nand_upgrade_ubinized $1;;
  326. + "ubifs") nand_upgrade_ubifs $1;;
  327. + *) nand_upgrade_tar $1;;
  328. + esac
  329. +}
  330. +
  331. +nand_upgrade_stage2() {
  332. + [ $1 = "nand" ] && {
  333. + [ -f "$2" ] && {
  334. + touch /tmp/sysupgrade
  335. +
  336. + killall -9 telnetd
  337. + killall -9 dropbear
  338. + killall -9 ash
  339. +
  340. + kill_remaining TERM
  341. + sleep 3
  342. + kill_remaining KILL
  343. +
  344. + sleep 1
  345. +
  346. + if [ -n "$(rootfs_type)" ]; then
  347. + v "Switching to ramdisk..."
  348. + run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
  349. + else
  350. + nand_do_upgrade_stage2 $2
  351. + fi
  352. + return 0
  353. + }
  354. + echo "Nand upgrade failed"
  355. + exit 1
  356. + }
  357. +}
  358. +
  359. +nand_upgrade_stage1() {
  360. + [ -f /tmp/sysupgrade-nand-path ] && {
  361. + path="$(cat /tmp/sysupgrade-nand-path)"
  362. + [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
  363. + rm $CONF_TAR
  364. +
  365. + ubus call system nandupgrade "{\"prefix\": \"$RAM_ROOT\", \"path\": \"$path\" }"
  366. + exit 0
  367. + }
  368. +}
  369. +append sysupgrade_pre_upgrade nand_upgrade_stage1
  370. +
  371. +# Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
  372. +# 3 types of files:
  373. +# 1) UBI - should contain an ubinized image, header is checked for the proper
  374. +# MAGIC
  375. +# 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
  376. +# header is checked for the proper MAGIC
  377. +# 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
  378. +# "CONTROL" file (at this point its content isn't verified)
  379. +#
  380. +# You usually want to call this function in platform_check_image.
  381. +#
  382. +# $(1): board name, used in case of passing TAR file
  383. +# $(2): file to be checked
  384. +nand_do_platform_check() {
  385. + local board_name="$1"
  386. + local tar_file="$2"
  387. + local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
  388. + local file_type="$(identify $2)"
  389. +
  390. + [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
  391. + echo "Invalid sysupgrade file."
  392. + return 1
  393. + }
  394. +
  395. + echo -n $2 > /tmp/sysupgrade-nand-path
  396. + cp /sbin/upgraded /tmp/
  397. +
  398. + return 0
  399. +}
  400. +
  401. +# Start NAND upgrade process
  402. +#
  403. +# $(1): file to be used for upgrade
  404. +nand_do_upgrade() {
  405. + echo -n $1 > /tmp/sysupgrade-nand-path
  406. + install_bin /sbin/upgraded
  407. + ln -s "$RAM_ROOT"/sbin/upgraded /tmp/upgraded
  408. + nand_upgrade_stage1
  409. +}
  410. diff --git a/package/system/procd/Makefile b/package/system/procd/Makefile
  411. index d343f7481f3bb627b3e7ad79f45864de82a42371..b95ef64b727f6f07c8f92c9caf027c21dbb30863 100644
  412. --- a/package/system/procd/Makefile
  413. +++ b/package/system/procd/Makefile
  414. @@ -24,7 +24,7 @@ PKG_LICENSE_FILES:=
  415. PKG_MAINTAINER:=John Crispin <blogic@openwrt.org>
  416. -PKG_CONFIG_DEPENDS:= CONFIG_KERNEL_SECCOMP CONFIG_NAND_SUPPORT CONFIG_PROCD_SHOW_BOOT CONFIG_PROCD_ZRAM_TMPFS \
  417. +PKG_CONFIG_DEPENDS:= CONFIG_KERNEL_SECCOMP CONFIG_PROCD_SHOW_BOOT CONFIG_PROCD_ZRAM_TMPFS \
  418. CONFIG_KERNEL_NAMESPACES CONFIG_PACKAGE_procd-ujail CONFIG_PACKAGE_procd-seccomp
  419. include $(INCLUDE_DIR)/package.mk
  420. @@ -35,7 +35,7 @@ TARGET_LDFLAGS += $(if $(CONFIG_USE_GLIBC),-lrt)
  421. define Package/procd
  422. SECTION:=base
  423. CATEGORY:=Base system
  424. - DEPENDS:=+ubusd +ubus +libjson-script +ubox +USE_GLIBC:librt +libubox +libubus +NAND_SUPPORT:procd-nand
  425. + DEPENDS:=+ubusd +ubus +libjson-script +ubox +USE_GLIBC:librt +libubox +libubus
  426. TITLE:=OpenWrt system process manager
  427. endef
  428. @@ -53,20 +53,6 @@ define Package/procd-seccomp
  429. TITLE:=OpenWrt process seccomp helper + utrace
  430. endef
  431. -define Package/procd-nand
  432. - SECTION:=utils
  433. - CATEGORY:=Utilities
  434. - DEPENDS:=@NAND_SUPPORT +ubi-utils
  435. - TITLE:=OpenWrt sysupgrade nand helper
  436. -endef
  437. -
  438. -define Package/procd-nand-firstboot
  439. - SECTION:=utils
  440. - CATEGORY:=Utilities
  441. - DEPENDS:=procd-nand
  442. - TITLE:=OpenWrt firstboot nand helper
  443. -endef
  444. -
  445. define Package/procd/config
  446. menu "Configuration"
  447. depends on PACKAGE_procd
  448. @@ -84,10 +70,6 @@ endmenu
  449. endef
  450. -ifeq ($(CONFIG_NAND_SUPPORT),y)
  451. - CMAKE_OPTIONS += -DBUILD_UPGRADED=1
  452. -endif
  453. -
  454. ifeq ($(CONFIG_PROCD_SHOW_BOOT),y)
  455. CMAKE_OPTIONS += -DSHOW_BOOT_ON_CONSOLE=1
  456. endif
  457. @@ -107,7 +89,7 @@ endif
  458. define Package/procd/install
  459. $(INSTALL_DIR) $(1)/sbin $(1)/etc $(1)/lib/functions
  460. - $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/{init,procd,askfirst,udevtrigger} $(1)/sbin/
  461. + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/{init,procd,askfirst,udevtrigger,upgraded} $(1)/sbin/
  462. $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/libsetlbf.so $(1)/lib
  463. $(INSTALL_BIN) ./files/reload_config $(1)/sbin/
  464. $(INSTALL_DATA) ./files/hotplug*.json $(1)/etc/
  465. @@ -126,21 +108,6 @@ define Package/procd-seccomp/install
  466. $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/libpreload-trace.so $(1)/lib
  467. endef
  468. -define Package/procd-nand/install
  469. - $(INSTALL_DIR) $(1)/sbin $(1)/lib/upgrade
  470. -
  471. - $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upgraded $(1)/sbin/
  472. - $(INSTALL_DATA) ./files/nand.sh $(1)/lib/upgrade/
  473. -endef
  474. -
  475. -define Package/procd-nand-firstboot/install
  476. - $(INSTALL_DIR) $(1)/lib/preinit
  477. -
  478. - $(INSTALL_DATA) ./files/nand-preinit.sh $(1)/lib/preinit/60-nand-firstboot.sh
  479. -endef
  480. -
  481. $(eval $(call BuildPackage,procd))
  482. $(eval $(call BuildPackage,procd-ujail))
  483. $(eval $(call BuildPackage,procd-seccomp))
  484. -$(eval $(call BuildPackage,procd-nand))
  485. -$(eval $(call BuildPackage,procd-nand-firstboot))
  486. diff --git a/package/system/procd/files/nand-preinit.sh b/package/system/procd/files/nand-preinit.sh
  487. deleted file mode 100644
  488. index cf596246d1f2891cbeb7b5c7cac4bb6e002b13fb..0000000000000000000000000000000000000000
  489. --- a/package/system/procd/files/nand-preinit.sh
  490. +++ /dev/null
  491. @@ -1,21 +0,0 @@
  492. -#!/bin/sh
  493. -# Copyright (C) 2014 OpenWrt.org
  494. -
  495. -nand_takeover() {
  496. - . /lib/upgrade/nand.sh
  497. - mtd=$(find_mtd_index "$CI_UBIPART")
  498. - esize=$(cat /proc/mtd | grep mtd$mtd |cut -d" " -f 3)
  499. - [ -z "$esize" ] && return 1
  500. - esize=$(printf "%d" 0x$esize)
  501. - for a in `seq 0 64`; do
  502. - mtd -o $((a * esize)) -l 400 dump /dev/mtd$mtd > /tmp/takeover.hdr
  503. - MAGIC=$(dd if=/tmp/takeover.hdr bs=1 skip=261 count=5 2> /dev/null)
  504. - SIZE=$(printf "%d" 0x$(dd if=/tmp/takeover.hdr bs=4 count=1 2> /dev/null | hexdump -v -n 4 -e '1/1 "%02x"'))
  505. - [ "$MAGIC" = "ustar" ] && {
  506. - mtd -o $((a * esize)) -l $((SIZE + 4)) dump /dev/mtd$mtd | dd bs=1 skip=4 of=/tmp/sysupgrade.tar
  507. - nand_do_upgrade_stage2 /tmp/sysupgrade.tar
  508. - }
  509. - done
  510. -}
  511. -
  512. -boot_hook_add initramfs nand_takeover
  513. diff --git a/package/system/procd/files/nand.sh b/package/system/procd/files/nand.sh
  514. deleted file mode 100644
  515. index ec2a014a00461ca897d521e2d065f5399f1f8c48..0000000000000000000000000000000000000000
  516. --- a/package/system/procd/files/nand.sh
  517. +++ /dev/null
  518. @@ -1,365 +0,0 @@
  519. -#!/bin/sh
  520. -# Copyright (C) 2014 OpenWrt.org
  521. -#
  522. -
  523. -. /lib/functions.sh
  524. -
  525. -# 'kernel' partition on NAND contains the kernel
  526. -CI_KERNPART="kernel"
  527. -
  528. -# 'ubi' partition on NAND contains UBI
  529. -CI_UBIPART="ubi"
  530. -
  531. -ubi_mknod() {
  532. - local dir="$1"
  533. - local dev="/dev/$(basename $dir)"
  534. -
  535. - [ -e "$dev" ] && return 0
  536. -
  537. - local devid="$(cat $dir/dev)"
  538. - local major="${devid%%:*}"
  539. - local minor="${devid##*:}"
  540. - mknod "$dev" c $major $minor
  541. -}
  542. -
  543. -nand_find_volume() {
  544. - local ubidevdir ubivoldir
  545. - ubidevdir="/sys/devices/virtual/ubi/$1"
  546. - [ ! -d "$ubidevdir" ] && return 1
  547. - for ubivoldir in $ubidevdir/${1}_*; do
  548. - [ ! -d "$ubivoldir" ] && continue
  549. - if [ "$( cat $ubivoldir/name )" = "$2" ]; then
  550. - basename $ubivoldir
  551. - ubi_mknod "$ubivoldir"
  552. - return 0
  553. - fi
  554. - done
  555. -}
  556. -
  557. -nand_find_ubi() {
  558. - local ubidevdir ubidev mtdnum
  559. - mtdnum="$( find_mtd_index $1 )"
  560. - [ ! "$mtdnum" ] && return 1
  561. - for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
  562. - [ ! -d "$ubidevdir" ] && continue
  563. - cmtdnum="$( cat $ubidevdir/mtd_num )"
  564. - [ ! "$mtdnum" ] && continue
  565. - if [ "$mtdnum" = "$cmtdnum" ]; then
  566. - ubidev=$( basename $ubidevdir )
  567. - ubi_mknod "$ubidevdir"
  568. - echo $ubidev
  569. - return 0
  570. - fi
  571. - done
  572. -}
  573. -
  574. -nand_get_magic_long() {
  575. - dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  576. -}
  577. -
  578. -get_magic_long_tar() {
  579. - ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
  580. -}
  581. -
  582. -identify_magic() {
  583. - local magic=$1
  584. - case "$magic" in
  585. - "55424923")
  586. - echo "ubi"
  587. - ;;
  588. - "31181006")
  589. - echo "ubifs"
  590. - ;;
  591. - "68737173")
  592. - echo "squashfs"
  593. - ;;
  594. - "d00dfeed")
  595. - echo "fit"
  596. - ;;
  597. - "4349"*)
  598. - echo "combined"
  599. - ;;
  600. - *)
  601. - echo "unknown $magic"
  602. - ;;
  603. - esac
  604. -}
  605. -
  606. -
  607. -identify() {
  608. - identify_magic $(nand_get_magic_long "$1" "${2:-0}")
  609. -}
  610. -
  611. -identify_tar() {
  612. - identify_magic $(get_magic_long_tar "$1" "$2")
  613. -}
  614. -
  615. -nand_restore_config() {
  616. - sync
  617. - local ubidev=$( nand_find_ubi $CI_UBIPART )
  618. - local ubivol="$( nand_find_volume $ubidev rootfs_data )"
  619. - [ ! "$ubivol" ] &&
  620. - ubivol="$( nand_find_volume $ubidev rootfs )"
  621. - mkdir /tmp/new_root
  622. - if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
  623. - echo "mounting ubifs $ubivol failed"
  624. - rmdir /tmp/new_root
  625. - return 1
  626. - fi
  627. - mv "$1" "/tmp/new_root/sysupgrade.tgz"
  628. - umount /tmp/new_root
  629. - sync
  630. - rmdir /tmp/new_root
  631. -}
  632. -
  633. -nand_upgrade_prepare_ubi() {
  634. - local rootfs_length="$1"
  635. - local rootfs_type="$2"
  636. - local has_kernel="${3:-0}"
  637. - local has_env="${4:-0}"
  638. -
  639. - local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
  640. - if [ ! "$mtdnum" ]; then
  641. - echo "cannot find ubi mtd partition $CI_UBIPART"
  642. - return 1
  643. - fi
  644. -
  645. - local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  646. - if [ ! "$ubidev" ]; then
  647. - ubiattach -m "$mtdnum"
  648. - sync
  649. - ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  650. - fi
  651. -
  652. - if [ ! "$ubidev" ]; then
  653. - ubiformat /dev/mtd$mtdnum -y
  654. - ubiattach -m "$mtdnum"
  655. - sync
  656. - ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  657. - [ "$has_env" -gt 0 ] && {
  658. - ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
  659. - ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
  660. - }
  661. - fi
  662. -
  663. - local kern_ubivol="$( nand_find_volume $ubidev kernel )"
  664. - local root_ubivol="$( nand_find_volume $ubidev rootfs )"
  665. - local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
  666. -
  667. - # remove ubiblock device of rootfs
  668. - local root_ubiblk="ubiblock${root_ubivol:3}"
  669. - if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
  670. - echo "removing $root_ubiblk"
  671. - if ! ubiblock -r /dev/$root_ubivol; then
  672. - echo "cannot remove $root_ubiblk"
  673. - return 1;
  674. - fi
  675. - fi
  676. -
  677. - # kill volumes
  678. - [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
  679. - [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
  680. - [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
  681. -
  682. - # update kernel
  683. - if [ "$has_kernel" = "1" ]; then
  684. - if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
  685. - echo "cannot create kernel volume"
  686. - return 1;
  687. - fi
  688. - fi
  689. -
  690. - # update rootfs
  691. - local root_size_param
  692. - if [ "$rootfs_type" = "ubifs" ]; then
  693. - root_size_param="-m"
  694. - else
  695. - root_size_param="-s $rootfs_length"
  696. - fi
  697. - if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
  698. - echo "cannot create rootfs volume"
  699. - return 1;
  700. - fi
  701. -
  702. - # create rootfs_data for non-ubifs rootfs
  703. - if [ "$rootfs_type" != "ubifs" ]; then
  704. - if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
  705. - echo "cannot initialize rootfs_data volume"
  706. - return 1
  707. - fi
  708. - fi
  709. - sync
  710. - return 0
  711. -}
  712. -
  713. -nand_do_upgrade_success() {
  714. - local conf_tar="/tmp/sysupgrade.tgz"
  715. -
  716. - sync
  717. - [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
  718. - echo "sysupgrade successful"
  719. - reboot -f
  720. -}
  721. -
  722. -# Flash the UBI image to MTD partition
  723. -nand_upgrade_ubinized() {
  724. - local ubi_file="$1"
  725. - local mtdnum="$(find_mtd_index "$CI_UBIPART")"
  726. -
  727. - [ ! "$mtdnum" ] && {
  728. - CI_UBIPART="rootfs"
  729. - mtdnum="$(find_mtd_index "$CI_UBIPART")"
  730. - }
  731. -
  732. - if [ ! "$mtdnum" ]; then
  733. - echo "cannot find mtd device $CI_UBIPART"
  734. - reboot -f
  735. - fi
  736. -
  737. - local mtddev="/dev/mtd${mtdnum}"
  738. - ubidetach -p "${mtddev}" || true
  739. - sync
  740. - ubiformat "${mtddev}" -y -f "${ubi_file}"
  741. - ubiattach -p "${mtddev}"
  742. - nand_do_upgrade_success
  743. -}
  744. -
  745. -# Write the UBIFS image to UBI volume
  746. -nand_upgrade_ubifs() {
  747. - local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
  748. -
  749. - nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
  750. -
  751. - local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  752. - local root_ubivol="$(nand_find_volume $ubidev rootfs)"
  753. - ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
  754. -
  755. - nand_do_upgrade_success
  756. -}
  757. -
  758. -nand_upgrade_tar() {
  759. - local tar_file="$1"
  760. - local board_name="$(cat /tmp/sysinfo/board_name)"
  761. - local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
  762. -
  763. - local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
  764. - local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
  765. -
  766. - local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)"
  767. -
  768. - local has_kernel=1
  769. - local has_env=0
  770. -
  771. - [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
  772. - tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - $CI_KERNPART
  773. - }
  774. - [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
  775. -
  776. - nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
  777. -
  778. - local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  779. - [ "$has_kernel" = "1" ] && {
  780. - local kern_ubivol="$(nand_find_volume $ubidev kernel)"
  781. - tar xf $tar_file sysupgrade-$board_name/kernel -O | \
  782. - ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
  783. - }
  784. -
  785. - local root_ubivol="$(nand_find_volume $ubidev rootfs)"
  786. - tar xf $tar_file sysupgrade-$board_name/root -O | \
  787. - ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
  788. -
  789. - nand_do_upgrade_success
  790. -}
  791. -
  792. -# Recognize type of passed file and start the upgrade process
  793. -nand_do_upgrade_stage2() {
  794. - local file_type=$(identify $1)
  795. -
  796. - [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
  797. -
  798. - case "$file_type" in
  799. - "ubi") nand_upgrade_ubinized $1;;
  800. - "ubifs") nand_upgrade_ubifs $1;;
  801. - *) nand_upgrade_tar $1;;
  802. - esac
  803. -}
  804. -
  805. -nand_upgrade_stage2() {
  806. - [ $1 = "nand" ] && {
  807. - [ -f "$2" ] && {
  808. - touch /tmp/sysupgrade
  809. -
  810. - killall -9 telnetd
  811. - killall -9 dropbear
  812. - killall -9 ash
  813. -
  814. - kill_remaining TERM
  815. - sleep 3
  816. - kill_remaining KILL
  817. -
  818. - sleep 1
  819. -
  820. - if [ -n "$(rootfs_type)" ]; then
  821. - v "Switching to ramdisk..."
  822. - run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
  823. - else
  824. - nand_do_upgrade_stage2 $2
  825. - fi
  826. - return 0
  827. - }
  828. - echo "Nand upgrade failed"
  829. - exit 1
  830. - }
  831. -}
  832. -
  833. -nand_upgrade_stage1() {
  834. - [ -f /tmp/sysupgrade-nand-path ] && {
  835. - path="$(cat /tmp/sysupgrade-nand-path)"
  836. - [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
  837. - rm $CONF_TAR
  838. -
  839. - ubus call system nandupgrade "{\"prefix\": \"$RAM_ROOT\", \"path\": \"$path\" }"
  840. - exit 0
  841. - }
  842. -}
  843. -append sysupgrade_pre_upgrade nand_upgrade_stage1
  844. -
  845. -# Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
  846. -# 3 types of files:
  847. -# 1) UBI - should contain an ubinized image, header is checked for the proper
  848. -# MAGIC
  849. -# 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
  850. -# header is checked for the proper MAGIC
  851. -# 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
  852. -# "CONTROL" file (at this point its content isn't verified)
  853. -#
  854. -# You usually want to call this function in platform_check_image.
  855. -#
  856. -# $(1): board name, used in case of passing TAR file
  857. -# $(2): file to be checked
  858. -nand_do_platform_check() {
  859. - local board_name="$1"
  860. - local tar_file="$2"
  861. - local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
  862. - local file_type="$(identify $2)"
  863. -
  864. - [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
  865. - echo "Invalid sysupgrade file."
  866. - return 1
  867. - }
  868. -
  869. - echo -n $2 > /tmp/sysupgrade-nand-path
  870. - cp /sbin/upgraded /tmp/
  871. -
  872. - return 0
  873. -}
  874. -
  875. -# Start NAND upgrade process
  876. -#
  877. -# $(1): file to be used for upgrade
  878. -nand_do_upgrade() {
  879. - echo -n $1 > /tmp/sysupgrade-nand-path
  880. - install_bin /sbin/upgraded
  881. - ln -s "$RAM_ROOT"/sbin/upgraded /tmp/upgraded
  882. - nand_upgrade_stage1
  883. -}