#!/bin/bash # # h3disp # # Patches fex/script.bin settings to modify display settings. Currently # rather limited and only supporting HDMI and the available presets. # ############################################################################# # # Background informations: # # Only a certain amount of predefined video modes is available on H3 devices # as can be seen here: http://linux-sunxi.org/Fex_Guide#disp_init_configuration # # Purpose of this script is to display the available reasonable settings and # let the user choose from them to adjust fex file accordingly afterwards. # In the meantime it's also possible to specify available resolutions on the # command line. To display possible values use the -h switch. # # If HDMI-to-DVI adapters are used another fix has to be applied to the fex # file: http://linux-sunxi.org/Orange_Pi_One#HDMI_to_DVI_converters -- so ask # user whether he plans to use such an adapter and adjust fex file accordingly # ############################################################################# # # CHANGES: # # v0.3: Added the ability to set the colour-range HDMI-output and spruced the # output of h3disp in general with a splash of colour. Also, don't spit # out so much text by default. # # v0.2: Added non-interactive mode. You can provide one or two arguments on # the command line, eg. 'h3disp -m 1080p60 -d' (1920x1080@60Hz DVI) or # 'h3disp -m 720i' (1280x720@30Hz HDMI). Complete list of modes via # 'h3disp -h' # # v0.1: Initial release to adjust display settings in script.bin on commonly # used Debian based OS images for H3 devices running Linux. # ############################################################################# # # TODO: # # - implement real user interaction: dialog that asks which of the available # HDMI modes to choose and whether a HDMI-to-DVI converter is used or not # # - maybe implement live resolution switching as outlined by Jernej: # http://forum.armbian.com/index.php/topic/617-wip-orange-pi-one-support-for-the-upcoming-orange-pi-one/?p=5305 # (we would also need a working set of fbset modes in /etc/fb.modes # of course) # # - maybe add the chosen mode to /boot/boot.cmd and create /boot/boot.scr # afterwards? # ############################################################################# Main() { export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # ensure script is running as root if [ "$(id -u)" != "0" ]; then echo "This script must be executed as root. Exiting" >&2 exit 1 fi # interactive mode if no arguments are provided -- currently not implemented # otherwise we try to parse the command line arguments and use these if [ $# -eq 0 ]; then DisplayUsage ; exit 0 # Provide a list with possible HDMI display settings and store the chosen # one in the HDMIMode variable : # ask the user whether he uses a HDMI-to-DVI converter and if true then # set DVIUsed=TRUE : else ParseOptions "$@" if [ -v ColourRange ]; then re='^[0-9]+$' if ! [[ ${ColourRange} =~ ${re} ]] ; then echo "$0: Invalid colour-range specified, only values 0-2 are supported for now!" exit 1 fi if [[ ${ColourRange} -lt 0 || ${ColourRange} -gt 2 ]] ; then echo "$0: Invalid colour-range specified, only values 0-2 are supported for now!" exit 1 fi fi case ${VideoMode} in 0|480i) # res HDMIMode=0 ;; 1|576i) # res HDMIMode=1 ;; 2|480p) # res HDMIMode=2 ;; 3|576p) # res HDMIMode=3 ;; 4|720p50|1280x720p50) # res HDMIMode=4 ;; 5|720p60|720p|1280x720p60) # res HDMIMode=5 ;; 6|1080i50|1920x1080i50) # res HDMIMode=6 ;; 7|1080i60|1080i|1920x1080i60) # res HDMIMode=7 ;; 8|1080p24|1920x1080p24) # res HDMIMode=8 ;; 9|1080p50|1920x1080p50) # res HDMIMode=9 ;; 10|1080p60|1080p|1920x1080p60) # res HDMIMode=10 ;; *) if [ "X${VideoMode}" = "X" ]; then echo -e "$0: missing video mode. Try one of the following:\n" else echo -e "$0: illegal video mode \"-m ${VideoMode}\". Try one of the following:\n" fi ShowVideoModes exit 0 ;; esac fi echo -e "Now trying to patch script.bin with your settings. \c" PatchScriptBin ${HDMIMode} ${DVIUsed} echo "Successfully finished. Please reboot for changes to take effect" # Let's see whether we have to collect debug output case ${Debug} in TRUE) which curl >/dev/null 2>&1 || apt-get -f -qq -y install curl echo -e "\nDebug output has been collected at the following URL: \c" (cat "${DebugOutput}"; echo -e "\n\n\nfex contents:\n" ; cat "${MyTmpFile}") \ | curl -F 'sprunge=<-' http://sprunge.us ;; esac } # Main ParseOptions() { while getopts 'hHvVdDm:M:c:C:' c ; do case ${c} in H) export FullUsage=TRUE DisplayUsage exit 0 ;; h) DisplayUsage exit 0 ;; v|V) # Increase verbosity. Will try to upload debug output from script # to ease reporting of bugs or strange behaviour. Use only when # asked for. export Debug=TRUE DebugOutput="$(mktemp /tmp/${0##*/}.XXXXXX)" trap "rm \"${DebugOutput}\" ; exit 0" 0 1 2 3 15 set -x exec 2>"${DebugOutput}" ;; d|D) # use HDMI-to-DVI converter export DVIUsed=TRUE ;; m|M) # The HDMI mode in question export VideoMode=${OPTARG} ;; c|C) # The colour-range in question export ColourRange=${OPTARG} ;; esac done } # ParseOptions DisplayUsage() { # check if stdout is a terminal... if test -t 1; then # see if it supports colors... ncolors=$(tput colors) if test -n "$ncolors" && test $ncolors -ge 8; then BOLD="$(tput bold)" NC='\033[0m' # No Color LGREEN='\033[1;32m' fi fi echo -e "Usage: ${BOLD}${0##*/} [-h/-H] -m [video mode] [-d] [-c [0-2]]${NC}\n" echo -e "############################################################################" if [ ${FullUsage} ]; then echo -e "\nDetailed Description:" grep "^#" "$0" | grep -v "^#\!/bin/bash" | sed 's/^#//' fi echo -e "\n This is a tool to set the display resolution of your Orange" echo -e " Pi by patching script.bin.\n\n In case you use an HDMI-to-DVI converter\c" echo -e " please use the ${LGREEN}-d${NC} switch.\n\n The resolution can be set using the ${LGREEN}-m${NC} switch.\c" echo -e " The following resolutions\n are currently supported:\n" ShowVideoModes echo -e " You can also specify the colour-range for your HDMI-display with the ${LGREEN}-c${NC} switch." echo -e "\n The following values for ${LGREEN}-c${NC} are currently supported:\n" echo -e " ${BOLD}0${NC} -- RGB range 16-255 (Default, use \"${LGREEN}-c 0${NC}\")" echo -e " ${BOLD}1${NC} -- RGB range 0-255 (Full range, use \"${LGREEN}-c 1${NC}\")" echo -e " ${BOLD}2${NC} -- RGB range 16-235 (Limited video, \"${LGREEN}-c 2${NC}\")\n" echo -e "############################################################################\n" } # DisplayUsage ShowVideoModes() { # check if stdout is a terminal... if test -t 1; then # see if it supports colors... ncolors=$(tput colors) if test -n "$ncolors" && test $ncolors -ge 8; then BOLD="$(tput bold)" NC='\033[0m' # No Color LGREEN='\033[1;32m' fi fi OIFS=${IFS} IFS="|" awk -F" " '/ # res/ {print $1}' <"${0}" | tr -d ')' | grep -v 'awk' | while read ; do set ${REPLY} echo -e " ${BOLD}${2}${NC}\tuse \"${LGREEN}-m ${2}${NC}\" or \"${LGREEN}-m ${1}${NC}\"" done IFS=${OIFS} echo -e "\n Two examples:\n\n '${BOLD}${0##*/}${NC} ${LGREEN}-m 1080p60 -d${NC}' (1920x1080@60Hz DVI)" echo -e " '${BOLD}${0##*/}${NC} ${LGREEN}-m 720i${NC}' (1280x720@30Hz HDMI)\n" } # ShowVideoModes PatchScriptBin() { # This function will be called with 2 arguments: # $1 HDMI mode to set # $2 wether HDMI-to-DVI converter should be used or not (has to be TRUE) # check whether we've the necessary tools available Fex2Bin="$(which fex2bin)" if [ "X${Fex2Bin}" = "X" ]; then apt-get -f -qq -y install sunxi-tools >/dev/null 2>&1 || InstallSunxiTools >/dev/null 2>&1 fi which fex2bin >/dev/null 2>&1 || (echo -e "Aborted\nfex2bin/bin2fex not found and unable to install. Exiting" >&2 ; exit 1) # try to find location of script.bin Path2ScriptBin=/boot if [ ! -f "${Path2ScriptBin}/script.bin" ]; then Path2ScriptBin="$(df | awk -F" " '/^\/dev\/mmcblk0p1/ {print $6}')" if [ ! -f "${Path2ScriptBin}/script.bin" ]; then echo -e "Aborted\nCan not find script.bin. Ensure boot partition is mounted" >&2 logger "Can not find script.bin. Ensure boot partition is mounted" exit 1 fi fi # resolve symlink to modify the original instead of the link ScriptBin="$(readlink -f "${Path2ScriptBin}/script.bin")" # create temp file MyTmpFile="$(mktemp /tmp/${0##*/}.XXXXXX)" # convert script.bin to temporary fex file local filter="^screen0_output" if [ -v ColourRange ]; then filter="^screen0_out"; fi bin2fex <"${ScriptBin}" 2>/dev/null | grep -v ${filter} \ | grep -v "^screen1_output" | grep -v "hdcp_enable" \ | grep -v "hdmi_cts_compatibility" >"${MyTmpFile}" if [ $? -ne 0 ]; then echo -e "Aborted\nCould not convert script.bin to fex. Exiting" >&2 logger "Could not convert script.bin to fex" exit 1 fi # create backup to be able to recover from failed conversion cp -p "${ScriptBin}" "${Path2ScriptBin}/script.bin.bak" sed -i "/\[disp_init\]/a screen0_output_type = 3\nscreen0_output_mode = $1\nscreen1_output_type = 3\nscreen1_output_mode = $1" "${MyTmpFile}" if [ -v ColourRange ]; then sed -i "/\[disp_init\]/a screen0_out_color_range = ${ColourRange}" "${MyTmpFile}" fi if [ "X$2" = "XTRUE" ]; then # add entries necessary for HDMI-to-DVI adapters sed -i '/\[hdmi_para\]/a hdcp_enable = 0\nhdmi_cts_compatibility = 1' "${MyTmpFile}" fi # convert temporary fex file back to script.bin fex2bin "${MyTmpFile}" "${ScriptBin}" 2>/dev/null if [ $? -ne 0 ]; then mv "${Path2ScriptBin}/script.bin.bak" "${ScriptBin}" echo -e "Aborted\nWriting script.bin went wrong. Nothing changed." >&2 echo -e "You may look at ${MyTmpFile} to see if there is an error there." >&2 logger "Writing script.bin went wrong. Nothing changed" exit 1 else rm "${MyTmpFile}" fi } # PatchScriptBin InstallSunxiTools() { sleep 1 apt-get -f -qq -y install libusb-1.0-0-dev || (echo -e "Aborted\nNot possible to install a sunxi-tools requirement" ; exit 1) cd /tmp git clone https://github.com/linux-sunxi/sunxi-tools cd sunxi-tools make make install } # InstallSunxiTools Main "$@"