Advertisement
iosx

Untitled

Oct 13th, 2018
1,396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. # #!/usr/bin/env fish
  2. # #
  3. # # Functions that interact with Astris/Kanzi. Recommend adding this to your ~/.config/fish/fish.config
  4. # #
  5. # # Useful for:
  6. # # - booting device in modes: iBoot, EFI diags (if available), fsboot, DFU
  7. # # - easy logging serial output to
  8. # #
  9. # # You can call arguments verbatim:
  10. # # $ fish kanzi.fish foobar a b
  11. # # Passed 2 args
  12. # # (1) a
  13. # # (2) b
  14. # #
  15. # # Author: Jason Sun <[email protected]>
  16. # #
  17. # #######################################################################################################################
  18.  
  19. # #######################################################################################################################
  20. # # Astris
  21. # # - simple wrappers on astris commands
  22. # #######################################################################################################################
  23.  
  24. # function astrisList
  25. # astrisctl list | grep KanziSWD
  26. # end
  27.  
  28. # function astrisConnect
  29. # if test (count $argv) -ne 1
  30. # astrisList
  31. # echo 'Give 1 target; like "B5"'
  32. # else
  33. # astris --stdio (astrisList | grep -E $argv)
  34. # end
  35. # end
  36.  
  37. # function astrisExec
  38. # if test (count $argv) -ne 2
  39. # astrisList
  40. # echo '<targets like "B5|0F"> and <cmd to send>'
  41. # else
  42. # set targets (astrisList | grep -E $argv[1])
  43. # set cmd $argv[2]
  44. # for astris in $targets
  45. # set_color blue; echo "$astris"; set_color normal;
  46. # begin
  47. # echo "$cmd"
  48. # end | astris --stdio $astris
  49. # end
  50. # end
  51. # end
  52.  
  53. # function astrisIsDiags
  54. # set ret (astrisExec $argv[1] is_in_efi)
  55. # echo $ret | grep -q True
  56. # end
  57.  
  58. # function astrisReset
  59. # if test (count $argv) -ne 1
  60. # astrisList
  61. # echo 'Give target(s); like regex "B5|0C", or the -all flag'
  62. # else if test $argv = "-all"
  63. # astrisList | xargs -I "{}" -t astrisctl reset --no-question --probe "{}"
  64. # else
  65. # astrisList | grep -E $argv | xargs -I "{}" -t astrisctl reset --no-question --probe "{}"
  66. # end
  67. # end
  68.  
  69. # # Prod-fused (PVT) devices doesn't support softdfu in astris
  70. # function astrisDFU
  71. # if test (count $argv) -eq 0
  72. # astrisList
  73. # echo 'Give target(s); like regex "B5|0C", or the -all flag'
  74. # else if test $argv = "-all"
  75. # for astris in astrisList
  76. # echo "softdfu" | astris --stdio $astris
  77. # end
  78. # else
  79. # set target $argv[1]
  80. # astrisReset $target
  81. # for astris in (astrisList | grep -E $target)
  82. # echo "dfu D21" | astris --stdio $astris
  83. # end
  84. # end
  85. # end
  86.  
  87. # #######################################################################################################################
  88. # # Communication (serial)
  89. # # - prefixed kdp because nanokdp
  90. # #######################################################################################################################
  91.  
  92. # # - different styles between astrisList (KanziSWD-3174E3) and kdpList (/dev/cu.kanzi-20A6F8)
  93. # function kdpList
  94. # nanokdp -s a | grep kanzi | awk '{ print $NF }' | sort
  95. # end
  96.  
  97. # function kdpConnect
  98. # if test (count $argv) -ne 1
  99. # kdpList
  100. # echo "Provide Kanzi serial target"
  101. # else
  102. # nanokdp -d (kdpList | grep $argv | awk '{ print $NF }')
  103. # end
  104. # end
  105.  
  106. # function kdpSSH
  107. # if test (count $argv) -ne 1
  108. # kdpList
  109. # echo "Provide Kanzi serial target"
  110. # return
  111. # end
  112. # if test ! -f "$HOME/.ssh/config"
  113. # echo "$HOME/.ssh/config file not found"
  114. # end
  115. # set udid ""
  116. # nanokdp -d (kdpList | grep $argv | awk '{ print $NF }')
  117. # end
  118.  
  119. # # Warning: Race condition exists between kdpExec's call completion and the call's output
  120. # # E.g. kdpExec F5 "printenv" translates to:
  121. # # `begin echo "printenv"; end | nanokdp -d /dev/cu.kanzi-20ABF5`
  122. # # Will print anywhere between 12, 15 lines
  123. # #
  124. # # tl;dr parsing output of kdpExec is not reliable
  125. # function kdpExec
  126. # if test (count $argv) -ne 2
  127. # echo 'Expects 2 args: (1) Give target(s); like regex "B5|0C" (2) command to exec; like "nvram -l"'
  128. # else
  129. # set targets $argv[1]
  130. # set cmd $argv[2]
  131. # for kanzi in (kdpList | grep -E $targets)
  132. # begin
  133. # # echo "nvram --set boot-command fsboot ; nvram --save ; reset"
  134. # echo $cmd
  135. # end | kdpConnect $kanzi
  136. # end
  137. # end
  138. # end
  139.  
  140. # #######################################################################################################################
  141. # # Booting to different iOS states
  142. # #######################################################################################################################
  143. # # What are the different iOS device states?
  144. # #
  145. # # - DFU
  146. # # 1. Connect the device to a computer.
  147. # # 2. Press and hold the Home + Lock buttons until the screen goes blank.
  148. # # 3. Count to 6.
  149. # # 4. While still holding the Home button, release the Lock button.
  150. # # - To get out: astrisctl reset --no-question --probe "KanziSWD-3174E3"
  151. # # - Or, Purple Restore
  152. # # - iBoot
  153. # # - With cable plugged in, hold lock+menu for 20s
  154. # # - Or, hold menu down while plugging in a cable
  155. # # - Or, from a serial prompt, hit enter to break while booting
  156. # # - diags
  157. # # - Plug into a Bacon UART cable and boot
  158. # # - Or, plug into a Kong cable set to diags mode and boot
  159. # # - Or, run the RootLoader action "Diags"
  160. # # - fsboot
  161. # # - There's a few options: look at this tree
  162. # #
  163. # # OS
  164. # # / \
  165. # # UI NonUI
  166. # # / \ / | \
  167. # # Internal Customer SwitchBoard Inferno EarthBound
  168. # #
  169. # #######################################################################################################################
  170.  
  171. # function kdpIBoot
  172. # if test (count $argv) -ne 1
  173. # kdpList
  174. # echo 'Give target(s); like regex "B5|0C"; or --all'
  175. # else
  176. # if test $argv[1] != '--all'
  177. # set targets (astrisctl list | grep KanziSWD | grep -E $argv)
  178. # else
  179. # set targets (astrisctl list | grep KanziSWD)
  180. # end
  181. # set_color blue;
  182. # set iter 1
  183. # for kanzi in $targets
  184. # set_color magenta; echo -e "("$iter")\tReset $kanzi"
  185. # astrisctl reset --no-question --probe "$kanzi" > /dev/null
  186. # set serial (echo "$kanzi" | cut -d'-' -f2)
  187. # # spam enter key during iboot, upto 2s; found empirically to be fairly reliable
  188. # for _ in (seq 20); kdpExec "$serial" "" > /dev/null; sleep 0.05; end
  189. # kdpExec $targets 'clearenv 1' > /dev/null 2>&1
  190. # kdpExec $targets 'saveenv' > /dev/null 2>&1
  191. # set iter (math $iter + 1)
  192. # end
  193. # end
  194. # end
  195.  
  196. # function kdpDiags
  197. # if test (count $argv) -ne 1
  198. # kdpList
  199. # echo 'Give target(s); like regex "B5|0C"'
  200. # else
  201. # set targets $argv[1]
  202. # kdpIBoot $targets
  203. # set_color green; echo "diags: $targets"
  204. # kdpExec $targets 'clearenv 1' > /dev/null 2>&1
  205. # kdpExec $targets 'setenv boot-command diags' > /dev/null 2>&1
  206. # kdpExec $targets 'saveenv' > /dev/null 2>&1
  207. # kdpExec $targets 'reboot' > /dev/null 2>&1
  208. # end
  209. # end
  210.  
  211. # function kdpFsboot
  212. # if test (count $argv) -ne 1
  213. # kdpList
  214. # echo 'Give target(s); like regex "B5|0C"'
  215. # else
  216. # set targets $argv[1]
  217. # kdpIBoot $targets
  218. # set_color green; echo "fsboot: $targets"
  219. # kdpExec $targets 'clearenv 1' > /dev/null 2>&1
  220. # kdpExec $targets 'setenv boot-command fsboot' > /dev/null 2>&1
  221. # kdpExec $targets 'saveenv' > /dev/null 2>&1
  222. # kdpExec $targets 'reboot' > /dev/null 2>&1
  223. # end
  224. # end
  225.  
  226. # # At the moment this cannot reliably detect state;
  227. # # device sometimes does not respond (i.e. when busy, when bricked), so it says iBoot a lot of times
  228. # function kdpQueryState
  229. # set targets (if test (count $argv) -eq 1; kdpList | egrep $argv[1]; else; kdpList; end)
  230. # for kanzi in $targets
  231. # set output (kdpExec $kanzi "" | strings)
  232. # if echo $output | grep -q "Darwin"
  233. # set_color green; echo "$kanzi Darwin"
  234. # else if echo $output | grep -q ":-)"
  235. # set_color blue; echo "$kanzi Diags"
  236. # else if echo $output | grep -q "]"
  237. # set_color cyan; echo "$kanzi iBoot"
  238. # echo "$output"
  239. # else
  240. # set_color red; echo "$kanzi unknown!"
  241. # echo "$output"
  242. # end
  243. # end
  244. # end
  245.  
  246. # #######################################################################################################################
  247. # # Logging
  248. # # - kdpLogDaemon: start logging a device's serial output to file
  249. # # - kdpKillDaemon: kills the log daemon
  250. # #######################################################################################################################
  251.  
  252. # function kdpLogDaemon
  253. # if test (count $argv) -ne 1
  254. # echo 'Provide Kanzi serial like "B5|0C|E3|14|BB"'
  255. # kdpList
  256. # else
  257. # set timestamp (date +%Y-%m-%d-%H-%M-%S)
  258. # for kanzi in (astrisctl list | grep KanziSWD | grep -E $argv)
  259. # set serial (echo "$kanzi" | cut -d'-' -f2)
  260. # set kdppath (nanokdp -s a | grep "$serial" | awk '{ print $NF }')
  261. # set outPath "/tmp/nanokdp.$kanzi.$timestamp.log"
  262. # echo "nanokdp -d $kdppath -L $outPath -l -N"
  263. # nanokdp -d "$kdppath" -L "$outPath" -l -N | grep 'process_id'
  264. # end
  265. # echo "Processes"
  266. # ps -x | grep nanokdp | grep $timestamp
  267. # echo "Logs"
  268. # ls -laht /tmp/nanokdp*.log | grep $timestamp
  269. # end
  270. # end
  271.  
  272. # function kdpKillDaemon
  273. # if test (count $argv) -ne 1
  274. # set_color red; echo 'Provide Kanzi serial like "B5|0C|E3|14|BB"'
  275. # kdpList
  276. # set_color blue; echo "Matched Processes"
  277. # ps -x | grep 'nanokdp' | grep '/dev'
  278. # else
  279. # echo "Matched Processes"
  280. # ps -x | grep nanokdp | grep -E $argv[1]
  281. # for pid in (ps -x | grep nanokdp | grep -E $argv[1] | cut -d' ' -f2)
  282. # echo "kill $pid"
  283. # kill $pid
  284. # end
  285. # end
  286. # end
  287.  
  288.  
  289. # #######################################################################################################################
  290. # # Example: foobar dummy call
  291.  
  292. # function foobar
  293. # echo 'Passed '(count $argv)' args'
  294. # set iter 1
  295. # for var in $argv
  296. # echo "($iter) $var"
  297. # set iter (math $iter + 1)
  298. # end
  299. # end
  300.  
  301. # eval "$argv"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement