flash_usb: Run dfu-util via sudo

Default to running dfu-util via sudo as most machines will not have
the user setup with permissions to access the raw usb device.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-03-11 22:13:20 -04:00
parent 12f6513ba2
commit 4718f39b2f
3 changed files with 12 additions and 8 deletions

View File

@ -70,22 +70,24 @@ def flash_bossac(device, binfile, extra_flags=[]):
pass pass
# Invoke the dfu-util program # Invoke the dfu-util program
def call_dfuutil(flags, binfile): def call_dfuutil(flags, binfile, sudo):
args = ["dfu-util"] + flags + ["-D", binfile] args = ["dfu-util"] + flags + ["-D", binfile]
if sudo:
args.insert(0, "sudo")
sys.stderr.write(" ".join(args) + '\n\n') sys.stderr.write(" ".join(args) + '\n\n')
res = subprocess.call(args) res = subprocess.call(args)
if res != 0: if res != 0:
raise error("Error running dfu-util") raise error("Error running dfu-util")
# Flash via a call to dfu-util # Flash via a call to dfu-util
def flash_dfuutil(device, binfile, extra_flags=[]): def flash_dfuutil(device, binfile, extra_flags=[], sudo=True):
hexfmt_r = re.compile(r"^[a-fA-F0-9]{4}:[a-fA-F0-9]{4}$") hexfmt_r = re.compile(r"^[a-fA-F0-9]{4}:[a-fA-F0-9]{4}$")
if hexfmt_r.match(device.strip()): if hexfmt_r.match(device.strip()):
call_dfuutil(["-d", ","+device.strip()] + extra_flags, binfile) call_dfuutil(["-d", ","+device.strip()] + extra_flags, binfile, sudo)
return return
buspath = translate_serial_to_usb_path(device) buspath = translate_serial_to_usb_path(device)
enter_bootloader(device) enter_bootloader(device)
call_dfuutil(["-p", buspath] + extra_flags, binfile) call_dfuutil(["-p", buspath] + extra_flags, binfile, sudo)
###################################################################### ######################################################################
@ -137,7 +139,7 @@ and then restart the Smoothieboard with that SD card.
def flash_lpc176x(options, binfile): def flash_lpc176x(options, binfile):
try: try:
flash_dfuutil(options.device, binfile) flash_dfuutil(options.device, binfile, [], options.sudo)
except error as e: except error as e:
sys.stderr.write(SMOOTHIE_HELP % (options.device, str(e))) sys.stderr.write(SMOOTHIE_HELP % (options.device, str(e)))
sys.exit(-1) sys.exit(-1)
@ -156,7 +158,7 @@ If attempting to flash via 3.3V serial, then use:
def flash_stm32f1(options, binfile): def flash_stm32f1(options, binfile):
try: try:
flash_dfuutil(options.device, binfile, ["-R", "-a", "2"]) flash_dfuutil(options.device, binfile, ["-R", "-a", "2"], options.sudo)
except error as e: except error as e:
sys.stderr.write(STM32F1_HELP % ( sys.stderr.write(STM32F1_HELP % (
options.device, str(e), options.device)) options.device, str(e), options.device))
@ -181,6 +183,8 @@ def main():
help="serial port device") help="serial port device")
opts.add_option("-o", "--offset", type="string", dest="offset", opts.add_option("-o", "--offset", type="string", dest="offset",
help="flash offset") help="flash offset")
opts.add_option("--no-sudo", action="store_false", dest="sudo",
default=True, help="do not run sudo")
options, args = opts.parse_args() options, args = opts.parse_args()
if len(args) != 1: if len(args) != 1:
opts.error("Incorrect number of arguments") opts.error("Incorrect number of arguments")

View File

@ -46,4 +46,4 @@ $(OUT)klipper.bin: $(OUT)klipper.elf
flash: $(OUT)klipper.bin flash: $(OUT)klipper.bin
@echo " Flashing $< to $(FLASH_DEVICE)" @echo " Flashing $< to $(FLASH_DEVICE)"
$(Q)$(PYTHON) ./scripts/flash_usb.py -t lpc176x -d "$(FLASH_DEVICE)" $(OUT)klipper.bin $(Q)$(PYTHON) ./scripts/flash_usb.py -t lpc176x -d "$(FLASH_DEVICE)" $(if $(NOSUDO),--no-sudo) $(OUT)klipper.bin

View File

@ -48,7 +48,7 @@ $(OUT)klipper.bin: $(OUT)klipper.elf
flash: $(OUT)klipper.bin flash: $(OUT)klipper.bin
@echo " Flashing $< to $(FLASH_DEVICE)" @echo " Flashing $< to $(FLASH_DEVICE)"
$(Q)$(PYTHON) ./scripts/flash_usb.py -t stm32f1 -d "$(FLASH_DEVICE)" $(OUT)klipper.bin $(Q)$(PYTHON) ./scripts/flash_usb.py -t stm32f1 -d "$(FLASH_DEVICE)" $(if $(NOSUDO),--no-sudo) $(OUT)klipper.bin
serialflash: $(OUT)klipper.bin serialflash: $(OUT)klipper.bin
@echo " Flashing $< to $(FLASH_DEVICE) via stm32flash" @echo " Flashing $< to $(FLASH_DEVICE) via stm32flash"