kanziswd # #!/usr/bin/env fish # # # # Functions that interact with Astris/Kanzi. Recommend adding this to your ~/.config/fish/fish.config # # # # Useful for: # # - booting device in modes: iBoot, EFI diags (if available), fsboot, DFU # # - easy logging serial output to # # # # You can call arguments verbatim: # # $ fish kanzi.fish foobar a b # # Passed 2 args kanziswd PasteShr kanziswd # # (1) a # # (2) b # # # # Author: Jason Sun # # # ####################################################################################################################### # ####################################################################################################################### # # Astris # # - simple wrappers on astris commands kanziswd PasteShr kanziswd # ####################################################################################################################### # function astrisList # astrisctl list | grep KanziSWD # end # function astrisConnect # if test (count $argv) -ne 1 # astrisList # echo 'Give 1 target; like "B5"' kanziswd How to dowload it? kanziswd # else # astris --stdio (astrisList | grep -E $argv) # end # end # function astrisExec # if test (count $argv) -ne 2 # astrisList # echo ' and ' # else kanziswd How to get it for free? kanziswd # set targets (astrisList | grep -E $argv[1]) # set cmd $argv[2] # for astris in $targets # set_color blue; echo "$astris"; set_color normal; # begin # echo "$cmd" # end | astris --stdio $astris # end # end # end kanziswd How to get it? kanziswd # function astrisIsDiags # set ret (astrisExec $argv[1] is_in_efi) # echo $ret | grep -q True # end # function astrisReset # if test (count $argv) -ne 1 # astrisList # echo 'Give target(s); like regex "B5|0C", or the -all flag' kanziswd How to get it for free? kanziswd # else if test $argv = "-all" # astrisList | xargs -I "{}" -t astrisctl reset --no-question --probe "{}" # else # astrisList | grep -E $argv | xargs -I "{}" -t astrisctl reset --no-question --probe "{}" # end # end # # Prod-fused (PVT) devices doesn't support softdfu in astris # function astrisDFU # if test (count $argv) -eq 0 kanziswd How to get it for free? kanziswd # astrisList # echo 'Give target(s); like regex "B5|0C", or the -all flag' # else if test $argv = "-all" # for astris in astrisList # echo "softdfu" | astris --stdio $astris # end # else # set target $argv[1] # astrisReset $target # for astris in (astrisList | grep -E $target) kanziswd How to get it? kanziswd # echo "dfu D21" | astris --stdio $astris # end # end # end # ####################################################################################################################### # # Communication (serial) # # - prefixed kdp because nanokdp # ####################################################################################################################### kanziswd How to get it? kanziswd # # - different styles between astrisList (KanziSWD-3174E3) and kdpList (/dev/cu.kanzi-20A6F8) # function kdpList # nanokdp -s a | grep kanzi | awk '{ print $NF }' | sort # end # function kdpConnect # if test (count $argv) -ne 1 # kdpList # echo "Provide Kanzi serial target" # else kanziswd PasteShr kanziswd # nanokdp -d (kdpList | grep $argv | awk '{ print $NF }') # end # end # function kdpSSH # if test (count $argv) -ne 1 # kdpList # echo "Provide Kanzi serial target" # return # end kanziswd PasteShr kanziswd # if test ! -f "$HOME/.ssh/config" # echo "$HOME/.ssh/config file not found" # end # set udid "" # nanokdp -d (kdpList | grep $argv | awk '{ print $NF }') # end # # Warning: Race condition exists between kdpExec's call completion and the call's output # # E.g. kdpExec F5 "printenv" translates to: # # `begin echo "printenv"; end | nanokdp -d /dev/cu.kanzi-20ABF5` kanziswd How to use it? kanziswd # # Will print anywhere between 12, 15 lines # # # # tl;dr parsing output of kdpExec is not reliable # function kdpExec # if test (count $argv) -ne 2 # echo 'Expects 2 args: (1) Give target(s); like regex "B5|0C" (2) command to exec; like "nvram -l"' # else # set targets $argv[1] # set cmd $argv[2] # for kanzi in (kdpList | grep -E $targets) kanziswd How to get it? kanziswd # begin # # echo "nvram --set boot-command fsboot ; nvram --save ; reset" # echo $cmd # end | kdpConnect $kanzi # end # end # end # ####################################################################################################################### # # Booting to different iOS states kanziswd How to get it? kanziswd # ####################################################################################################################### # # What are the different iOS device states? # # # # - DFU # # 1. Connect the device to a computer. # # 2. Press and hold the Home + Lock buttons until the screen goes blank. # # 3. Count to 6. # # 4. While still holding the Home button, release the Lock button. # # - To get out: astrisctl reset --no-question --probe "KanziSWD-3174E3" # # - Or, Purple Restore kanziswd PasteShr kanziswd # # - iBoot # # - With cable plugged in, hold lock+menu for 20s # # - Or, hold menu down while plugging in a cable # # - Or, from a serial prompt, hit enter to break while booting # # - diags # # - Plug into a Bacon UART cable and boot # # - Or, plug into a Kong cable set to diags mode and boot # # - Or, run the RootLoader action "Diags" # # - fsboot # # - There's a few options: look at this tree kanziswd How to use it? kanziswd # # # # OS # # / \ # # UI NonUI # # / \ / | \ # # Internal Customer SwitchBoard Inferno EarthBound # # # ####################################################################################################################### # function kdpIBoot kanziswd How to get it? kanziswd # if test (count $argv) -ne 1 # kdpList # echo 'Give target(s); like regex "B5|0C"; or --all' # else # if test $argv[1] != '--all' # set targets (astrisctl list | grep KanziSWD | grep -E $argv) # else # set targets (astrisctl list | grep KanziSWD) # end # set_color blue; kanziswd PasteShr kanziswd # set iter 1 # for kanzi in $targets # set_color magenta; echo -e "("$iter")\tReset $kanzi" # astrisctl reset --no-question --probe "$kanzi" > /dev/null # set serial (echo "$kanzi" | cut -d'-' -f2) # # spam enter key during iboot, upto 2s; found empirically to be fairly reliable # for _ in (seq 20); kdpExec "$serial" "" > /dev/null; sleep 0.05; end # kdpExec $targets 'clearenv 1' > /dev/null 2>&1 # kdpExec $targets 'saveenv' > /dev/null 2>&1 # set iter (math $iter + 1) kanziswd How to get it? kanziswd # end # end # end # function kdpDiags # if test (count $argv) -ne 1 # kdpList # echo 'Give target(s); like regex "B5|0C"' # else # set targets $argv[1] kanziswd How to use it? kanziswd # kdpIBoot $targets # set_color green; echo "diags: $targets" # kdpExec $targets 'clearenv 1' > /dev/null 2>&1 # kdpExec $targets 'setenv boot-command diags' > /dev/null 2>&1 # kdpExec $targets 'saveenv' > /dev/null 2>&1 # kdpExec $targets 'reboot' > /dev/null 2>&1 # end # end # function kdpFsboot kanziswd How to use it? kanziswd # if test (count $argv) -ne 1 # kdpList # echo 'Give target(s); like regex "B5|0C"' # else # set targets $argv[1] # kdpIBoot $targets # set_color green; echo "fsboot: $targets" # kdpExec $targets 'clearenv 1' > /dev/null 2>&1 # kdpExec $targets 'setenv boot-command fsboot' > /dev/null 2>&1 # kdpExec $targets 'saveenv' > /dev/null 2>&1 kanziswd How to get it for free? kanziswd # kdpExec $targets 'reboot' > /dev/null 2>&1 # end # end # # At the moment this cannot reliably detect state; # # device sometimes does not respond (i.e. when busy, when bricked), so it says iBoot a lot of times # function kdpQueryState # set targets (if test (count $argv) -eq 1; kdpList | egrep $argv[1]; else; kdpList; end) # for kanzi in $targets # set output (kdpExec $kanzi "" | strings) kanziswd How to dowload it? kanziswd # if echo $output | grep -q "Darwin" # set_color green; echo "$kanzi Darwin" # else if echo $output | grep -q ":-)" # set_color blue; echo "$kanzi Diags" # else if echo $output | grep -q "]" # set_color cyan; echo "$kanzi iBoot" # echo "$output" # else # set_color red; echo "$kanzi unknown!" # echo "$output" kanziswd How to use it? kanziswd # end # end # end # ####################################################################################################################### # # Logging # # - kdpLogDaemon: start logging a device's serial output to file # # - kdpKillDaemon: kills the log daemon # ####################################################################################################################### kanziswd How to dowload it? kanziswd # function kdpLogDaemon # if test (count $argv) -ne 1 # echo 'Provide Kanzi serial like "B5|0C|E3|14|BB"' # kdpList # else # set timestamp (date +%Y-%m-%d-%H-%M-%S) # for kanzi in (astrisctl list | grep KanziSWD | grep -E $argv) # set serial (echo "$kanzi" | cut -d'-' -f2) # set kdppath (nanokdp -s a | grep "$serial" | awk '{ print $NF }') # set outPath "/tmp/nanokdp.$kanzi.$timestamp.log" kanziswd How to use it? kanziswd # echo "nanokdp -d $kdppath -L $outPath -l -N" # nanokdp -d "$kdppath" -L "$outPath" -l -N | grep 'process_id' # end # echo "Processes" # ps -x | grep nanokdp | grep $timestamp # echo "Logs" # ls -laht /tmp/nanokdp*.log | grep $timestamp # end # end kanziswd How to use it? kanziswd # function kdpKillDaemon # if test (count $argv) -ne 1 # set_color red; echo 'Provide Kanzi serial like "B5|0C|E3|14|BB"' # kdpList # set_color blue; echo "Matched Processes" # ps -x | grep 'nanokdp' | grep '/dev' # else # echo "Matched Processes" # ps -x | grep nanokdp | grep -E $argv[1] # for pid in (ps -x | grep nanokdp | grep -E $argv[1] | cut -d' ' -f2) kanziswd PasteShr kanziswd # echo "kill $pid" # kill $pid # end # end # end # ####################################################################################################################### # # Example: foobar dummy call kanziswd PasteShr kanziswd # function foobar # echo 'Passed '(count $argv)' args' # set iter 1 # for var in $argv # echo "($iter) $var" # set iter (math $iter + 1) # end # end # eval "$argv" kanziswd How to dowload it? kanziswd kanziswd