freedombone-image-hardware-setup 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/bin/sh
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Hardware setup based on bin/freedombox-hardware-setup from freedom-maker
  12. #
  13. # License
  14. # =======
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-image-hardware-setup
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. enable_serial_console() {
  32. # By default, spawn a console on the serial port
  33. device="$1"
  34. echo $"Adding a getty on the serial port"
  35. echo "T0:12345:respawn:/sbin/getty -L $device 115200 vt100" >> /etc/inittab
  36. }
  37. beaglebone_setup_boot() {
  38. bbb_version=$1
  39. # Setup uEnv.txt
  40. if grep -q btrfs /etc/fstab ; then
  41. fstype=btrfs
  42. else
  43. fstype=ext4
  44. fi
  45. if [ ! $bbb_version ]; then
  46. kernelVersion=$(ls /usr/lib/*/am335x-boneblack.dtb | head -1 | cut -d/ -f4)
  47. else
  48. kernelVersion=$(ls /usr/lib/*/am335x-boneblack-${1}.dtb | head -1 | cut -d/ -f4)
  49. fi
  50. version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
  51. initRd=initrd.img-$version
  52. vmlinuz=vmlinuz-$version
  53. bbb_loadaddr='0x82000000'
  54. bbb_initrd_addr='0x88080000'
  55. bbb_fdtaddr='0x88000000'
  56. # uEnv.txt for Beaglebone
  57. # based on https://github.com/beagleboard/image-builder/blob/master/target/boot/beagleboard.org.txt
  58. cat >> /boot/uEnv.txt <<EOF
  59. mmcroot=/dev/mmcblk0p2 ro
  60. mmcrootfstype=$fstype rootwait fixrtc
  61. mmcrootflags=subvol=@
  62. console=ttyO0,115200n8
  63. kernel_file=$vmlinuz
  64. initrd_file=$initRd
  65. loadaddr=$bbb_loadaddr
  66. initrd_addr=$bbb_initrd_addr
  67. fdtaddr=$bbb_fdtaddr
  68. initrd_high=0xffffffff
  69. fdt_high=0xffffffff
  70. loadkernel=load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
  71. loadinitrd=load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}; setenv initrd_size \${filesize}
  72. loadfdt=load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
  73. loadfiles=run loadkernel; run loadinitrd; run loadfdt
  74. mmcargs=setenv bootargs init=/lib/systemd/systemd console=tty0 console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags} ifnames=0 slub_debug=FZP slab_nomerge page_poison=1
  75. uenvcmd=run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
  76. EOF
  77. mkdir -p /boot/dtbs
  78. cp /usr/lib/linux-image-*-armmp/* /boot/dtbs
  79. }
  80. beaglebone_flash() {
  81. bbb_version=$1
  82. # allow flash-kernel to work without valid /proc contents
  83. # ** this doesn't *really* work, since there are too many checks
  84. # that fail in an emulated environment! We'll have to do it by
  85. # hand below anyway...
  86. if [[ "$bbb_version" == 'wireless' ]]; then
  87. export FK_MACHINE="TI AM335x BeagleBone Black Wireless"
  88. else
  89. export FK_MACHINE="TI AM335x BeagleBone Black"
  90. fi
  91. apt-get install -y flash-kernel
  92. }
  93. beaglebone_repack_kernel() {
  94. bbb_version=$1
  95. # process installed kernel to create uImage, uInitrd, dtb
  96. # using flash-kernel would be a good approach, except it fails in the
  97. # cross build environment due to too many environment checks...
  98. #FK_MACHINE="TI AM335x BeagleBone" flash-kernel
  99. # so, let's do it manually...
  100. # flash-kernel's hook-functions provided to mkinitramfs have the
  101. # unfortunate side-effect of creating /conf/param.conf in the initrd
  102. # when run from our emulated chroot environment, which means our root=
  103. # on the kernel command line is completely ignored! repack the initrd
  104. # to remove this evil...
  105. echo "info: repacking beaglebone kernel and initrd"
  106. bbb_dtb='am335x-boneblack'
  107. if [ $bbb_version ]; then
  108. bbb_dtb="am335x-boneblack-${bbb_version}"
  109. fi
  110. kernelVersion=$(ls /usr/lib/*/${bbb_dtb}.dtb | head -1 | cut -d/ -f4)
  111. version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
  112. initRd=initrd.img-$version
  113. vmlinuz=vmlinuz-$version
  114. # optionally use a separately compiled kernel
  115. bbb_dtb_file=/usr/lib/$kernelVersion/${bbb_dtb}.dtb
  116. #if [ -f /boot/bbb.tar.gz ]; then
  117. # cd /boot
  118. # tar -xzvf /boot/bbb.tar.gz
  119. # if [ -f /boot/bbb/dtbs/${bbb_dtb}.dtb ]; then
  120. # if [ -f /boot/bbb/zImage ]; then
  121. # bbb_dtb_file=/boot/bbb/dtbs/${bbb_dtb}.dtb
  122. # vmlinuz=/boot/bbb/zImage
  123. # fi
  124. # fi
  125. #fi
  126. mkdir /tmp/initrd-repack
  127. (cd /tmp/initrd-repack ; \
  128. zcat /boot/$initRd | cpio -i ; \
  129. rm -f conf/param.conf ; \
  130. find . | cpio --quiet -o -H newc | \
  131. gzip -9 > /boot/$initRd )
  132. rm -rf /tmp/initrd-repack
  133. (cd /boot ; \
  134. cp ${bbb_dtb_file} dtb ; \
  135. cat $vmlinuz dtb >> temp-kernel ; \
  136. mkimage -A arm -O linux -T kernel -n "Debian kernel ${version}" \
  137. -C none -a 0x82000000 -e 0x82000000 -d temp-kernel uImage ; \
  138. rm -f temp-kernel ; \
  139. mkimage -A arm -O linux -T ramdisk -C gzip -a 0x88080000 -e 0x88080000 \
  140. -n "Debian ramdisk ${version}" \
  141. -d $initRd uInitrd )
  142. }
  143. a20_setup_boot() {
  144. dtb="$1"
  145. # Setup boot.cmd
  146. if grep -q btrfs /etc/fstab ; then
  147. fstype=btrfs
  148. else
  149. fstype=ext4
  150. fi
  151. kernelVersion=$(ls /usr/lib/*/$dtb | head -1 | cut -d/ -f4)
  152. version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
  153. initRd=initrd.img-$version
  154. vmlinuz=vmlinuz-$version
  155. # Create boot.cmd
  156. cat >> /boot/boot.cmd <<EOF
  157. setenv mmcdev 0
  158. setenv mmcpart 1
  159. setenv mmcroot /dev/mmcblk0p2 ro
  160. setenv mmcrootfstype $fstype rootwait fixrtc
  161. setenv mmcrootflags subvol=@
  162. setenv console ttyS0,115200n8
  163. setenv kernel_file $vmlinuz
  164. setenv initrd_file $initRd
  165. setenv fdtfile $dtb
  166. setenv loadaddr 0x46000000
  167. setenv initrd_addr 0x48000000
  168. setenv fdtaddr 0x47000000
  169. setenv initrd_high 0xffffffff
  170. setenv fdt_high 0xffffffff
  171. setenv loadkernel load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
  172. setenv loadinitrd load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}\\; setenv initrd_size \\\${filesize}
  173. setenv loadfdt load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
  174. setenv loadfiles run loadkernel\\; run loadinitrd\\; run loadfdt
  175. setenv mmcargs setenv bootargs init=/lib/systemd/systemd console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags} ifnames=0 slub_debug=FZP slab_nomerge page_poison=1
  176. run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
  177. EOF
  178. # boot.scr for Allwinner A20 based device
  179. mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
  180. # Copy all DTBs
  181. mkdir -p /boot/dtbs
  182. cp /usr/lib/$kernelVersion/* /boot/dtbs
  183. # extra boot modules
  184. echo "rtc_sunxi" >> /etc/initramfs-tools/modules
  185. }
  186. setup_flash_kernel() {
  187. if [ ! -d /etc/flash-kernel ] ; then
  188. mkdir /etc/flash-kernel
  189. fi
  190. echo -n "$1" > /etc/flash-kernel/machine
  191. command_line=""
  192. if [ -n "$2" ] ; then
  193. command_line="console=$2"
  194. fi
  195. if [ -n "$command_line" ] ; then
  196. echo flash-kernel flash-kernel/linux_cmdline string "$command_line" | debconf-set-selections
  197. fi
  198. apt-get install -y flash-kernel
  199. }
  200. case "$MACHINE" in
  201. beaglebone)
  202. beaglebone_setup_boot
  203. beaglebone_flash
  204. beaglebone_repack_kernel
  205. enable_serial_console ttyO0
  206. ;;
  207. beaglebonewifi)
  208. beaglebone_setup_boot wireless
  209. beaglebone_flash wireless
  210. beaglebone_repack_kernel wireless
  211. enable_serial_console ttyO0
  212. ;;
  213. cubietruck)
  214. a20_setup_boot sun7i-a20-cubietruck.dtb
  215. enable_serial_console ttyS0
  216. ;;
  217. a20-olinuxino-lime)
  218. a20_setup_boot sun7i-a20-olinuxino-lime.dtb
  219. enable_serial_console ttyS0
  220. ;;
  221. a20-olinuxino-lime2)
  222. a20_setup_boot sun7i-a20-olinuxino-lime2.dtb
  223. enable_serial_console ttyS0
  224. ;;
  225. a20-olinuxino-micro)
  226. a20_setup_boot sun7i-a20-olinuxino-micro.dtb
  227. enable_serial_console ttyS0
  228. ;;
  229. cubieboard2)
  230. a20_setup_boot sun7i-a20-cubieboard2.dtb
  231. enable_serial_console ttyS0
  232. ;;
  233. pcduino3)
  234. a20_setup_boot sun7i-a20-pcduino3.dtb
  235. enable_serial_console ttyS0
  236. ;;
  237. esac