Skocz do zawartości

ESP32 + esptool.py, flaszowanie firmware micropythona (i nie tylko) za pomocą smartfona z OTG (nie tutorial)


Pomocna odpowiedź

@H1M4W4R1 , tak, fajne narzędzie, można wykorzystać na różne sposoby. Potrafi też np. scalić .bin spod różnych adresów w jeden pełny obraz, który można wgrać bezpośrednio flashrom-em, jeżeli mamy dostęp do kostki (np. w lolin32 lite). Generalnie flashrom oczekuje rozmiaru obrazu pokrywającego się z rozmiarem docelowej pamięci (nawet przy "wycinkach"), a esptool radzi sobie z tym bdb.

  • Lubię! 1
  • 2 tygodnie później...

Dodałem kilka poprawek w skrypcie "qedit". Przydałby się nowy wątek, bo tematem odbiega, ale czy jest sens zakładać.

#!/data/data/com.termux/files/usr/bin/bash

### Info ###
# 1. Zainstaluj wymagane pakiety, wpisz:
#     pkg i coreutils gawk jq termux-tools
#     jeżeli posiadasz root-a: pkg i tsu
# 2. Ustaw (odkomentuj) w konfigu "$HOME/.termux/termux.properties" wartość: "allow-external-apps = true"
# 3. Zapisz i przeładuj konfig poleceniem: "termux-reload-settings"

# flagi
root_mode=0
block_mode=0
del_auto=1
del_files=1
is_stdin=1

# błędy
errors=0

# tablice
declare -A new_files=()
declare -A skip_files=()
declare -a order=()

usage(){
	local sn="${BASH_SOURCE##*/}"
	cat << EOF
Użycie:
 $sn [opcje] <plik1> [plik2 ...]

Opcje:
 [-r|--root]       - uruchom jako root (wymaga sudo/tsu)
 [-b|--block]      - tryb blokujący (dla rshell/mpremote)
 [-m|--man-del]    - pytaj czy usunąć nowe, puste pliki
 [-n|--no-del]     - nigdy nie usuwaj pustych plików
 
 [-i|--install]    - zainstaluj skrypt w systemie
 [-u|--uninstall]  - odinstaluj skrypt z systemu
 
 [-h|--help]       - wyświetl pomoc
 
Przykłady:
 $sn -r -b -m plik1.txt plik2.txt
 $sn --no-del plik1.txt
 $sn plik?.txt
 $sn pl*.txt
 $sn *.txt
 $sn *
EOF
	exit "${1:-1}"
}

install_script(){
	local src="$(realpath "$BASH_SOURCE")" dest="${PREFIX}/bin/qedit" msg=""
	if [[ "$src" == "$dest" ]]; then
		echo "[!] instalacja anulowana (wywołano skrypt systemowy)"; exit 1
	fi
	if [[ -f "$dest" ]]; then
		if cmp -s "$src" "$dest"; then
			echo "[i] bieżąca wersja jest już zainstalowana"
			if [[ "$(stat -c "%a" "$dest" 2> /dev/null)" != "700" ]]; then
				if chmod 700 "$dest" 2> /dev/null; then
					echo "[i] naprawiono uprawnienia"
				else
					echo "[!] błąd naprawy uprawnień"; exit 1
				fi
			fi
			exit 0
		fi
		echo "[?] skrypt docelowy istnieje, nadpisać? (t/n)"
		local choice=""
		until [[ "$choice" == [tTnN] ]]; do 
			if ! read -r -s -n 1 choice; then break; fi
		done
		if [[ "$choice" != [tT] ]]; then exit 0; fi
		msg="zaktualizowano"
	else
		msg="zainstalowano"
	fi
	if install -m 700 "$src" "$dest" 2> /dev/null; then
		echo "[i] ${msg}"
	else
		echo "[!] błąd instalacji"; exit 1
	fi
	exit 0
}

uninstall_script(){
	local src="$(realpath "$BASH_SOURCE")" dest="${PREFIX}/bin/qedit"
	if [[ "$src" == "$dest" ]]; then echo "[!] deinstalacja anulowana (wywołano skrypt systemowy)"; exit 1; fi
	if [[ -f "$dest" ]]; then
		if rm -- "$dest" &> /dev/null; then
			echo "[i] odinstalowano"; exit 0
		else
			echo "[!] błąd deinstalacji"
		fi
	else
		echo "[i] skrypt nie jest zainstalowany"
	fi
	exit 1
}

cleanup(){
	trap - EXIT
	term_fix
	local choice=""
	for f in "${order[@]}"; do
		if [[ -f "$f" && ! -s "$f" ]]; then
			if (( del_auto )); then
				echo "[x] '${new_files[$f]}' jest pusty, usuwam"
				if ! rm -f -- "$f" 2> /dev/null; then echo "[!] błąd usuwania pliku '$f'"; fi
			else
				echo "[?] '${new_files[$f]}' jest pusty, usunąć (t/n)"
				choice=""
				until [[ "$choice" == [tTnN] ]]; do 
					if ! read -r -s -n 1 choice; then break; fi
				done
				if [[ "$choice" == [tT] ]]; then
					if ! rm -f -- "$f" 2> /dev/null; then echo "[!] błąd usuwania pliku '$f'"; fi
				fi
			fi
		fi
	done
	if (( errors > 0 )); then exit 1; else exit 0; fi
}

no_cleanup(){
	trap - EXIT
	term_fix
	if (( errors > 0 )); then exit 1; else exit 0; fi
}

sigint_handler(){
	trap - INT
	term_fix
	echo "[i] wychodzę ..."
	exit
}

term_fix(){
	if (( is_stdin )); then stty echo 2> /dev/null; fi
}

check_stdin(){
	if [[ -t 0 ]]; then
		is_stdin=1
	else
		is_stdin=0
		block_mode=0
		del_auto=1
	fi
}

set_traps(){
	trap "" HUP
	trap "sigint_handler" INT
	if (( del_files )); then trap "cleanup" EXIT; else trap "no_cleanup" EXIT; fi
}

while (( "$#" )); do
	case "$1" in
		-h|--help) usage 0 ;;
		-i|--install) install_script ;;
		-u|--uninstall) uninstall_script ;;
		-r|--root) root_mode=1; shift ;;
		-b|--block) block_mode=1; shift ;;
		-m|--man-del) del_auto=0; shift ;;
		-n|--no-del) del_files=0; shift ;;
		--) shift; break ;;
		-*) echo "[!] nieznana opcja '$1'" >&2; exit 1 ;;
		*) break ;;
	esac
done
if (( ! "$#" )); then usage; fi

# ważne funkcje
check_stdin
set_traps

if (( root_mode )); then 
	if [[ -z "$(command -v sudo)" || "$(sudo id -u 2> /dev/null)" != "0" ]]; then echo "[!] root niedostępny" >&2; exit 1; fi
fi

apk="$(cmd package list packages -3 -e | gawk -F':' '/^package:com\.rhmsoft\.edit(\.pro)?$/{print $2; exit}')"
if [[ -z "$apk" ]]; then
	echo "[!] włącz/zainstaluj 'QuickEdit'" >&2; exit 1
fi

for arg in "$@"; do
	file=$(realpath -m "$arg" 2> /dev/null)
	if [[ -z "$file" ]]; then echo "[!] błąd 'realpath', pusta ścieżka dla '${arg}'" >&2; (( errors++ )); continue; fi
	if (( del_files )); then
		if [[ -v skip_files["$file"] ]]; then continue; else skip_files["$file"]="$arg"; fi
	fi
	
	dir=$(dirname -- "$file")
	if [[ ! -d "$dir" ]]; then echo "[!] ścieżka pliku '${arg}' nie istnieje" >&2; (( errors++ )); continue; fi
	if [[ -d "$file" ]]; then echo "[i] pomijam katalog '${arg%/}'" >&2; (( errors++ )); continue; fi
	
	if [[ ! -f "$file" ]]; then
		if touch -- "$file" 2> /dev/null; then
			if (( del_files )); then
				order+=("$file")
				new_files["$file"]="$arg"
			fi
		else
			echo "[!] nie można utworzyć '${arg}'" >&2; (( errors++ )); continue
		fi
	fi

	if (( root_mode )); then
		sudo am start -W -n "${apk}/com.rhmsoft.edit.activity.MainActivity" \
		-a "android.intent.action.EDIT" -d "file://${file}" -t "text/plain" &> /dev/null
	else
		uri="content://com.termux.documents/document/$(printf '%s' "$file" | jq -sRr @uri)"
		am start -W --user current -a "android.intent.action.EDIT" -d "$uri" -t "text/plain" \
		--grant-read-uri-permission --grant-write-uri-permission \
		--grant-persistable-uri-permission \
		-p "$apk" &> /dev/null
	fi
	if (( "$?" )); then (( errors++ )); echo "[!] nie można uruchomić 'QuickEdit' dla '${arg}'" >&2; fi
done

if (( is_stdin && ( block_mode || ${#order[@]} ) )); then
	echo "[>] zapisz dane w 'QuickEdit' i naciśnij 'q'"
	quit=""
	until [[ "$quit" == [qQ] ]]; do if ! read -r -s -n 1 quit; then break; fi; done
fi

 

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...