Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuse and lock bits reading #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

budvar10
Copy link

Implementation of STK500 Universal Command for BIGBOOT setup instead of dummy functionality; the bootlader can return real fuse and lock bits instead of zeroes (avrdude -v parameter).

Implementation of STK500 Universal Command for BIGBOOT setup instead of  dummy functionality; the bootlader can return real fuse and lock bits instead of zeroes (avrdude -v parameter).
@hecko
Copy link

hecko commented Dec 22, 2022

Is this something to review and is considered to be interesting for the project @WestfW ?

@budvar10 can you maybe review this pull request and provide a little more information on how do you propose to test your patch against real hardware - for example with avrdude - like comparing avrdude with atmel-ice ISP programmer output and avrdude with adruino programmer maybe?

@mcuee
Copy link

mcuee commented Dec 23, 2022

The repo (https://github.com/budvar10/optiboot/tree/patch-1) does not seem to build correctly under git bash + Atmel toochain (tried both avr-gcc 5.4.0 version and 7.3.0 version).

MINGW64 /c/work/avr/avrdude_test/others/optiboot_pr210/optiboot/bootloaders/optiboot (patch-1)
$ make atmega328 BIGBOOT=1
avr-gcc (AVR_8_bit_GNU_Toolchain_3.6.2_1778) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

BAUD RATE CHECK: Desired: 115200, Real: 117647, UBRRL = 16, Error=2.1%
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3 -DBIGBOOT=1       -c -o optiboot.o optiboot.c
optiboot.c:305:6: warning: #warning BAUD_RATE error greater than 2% [-Wcpp]
     #warning BAUD_RATE error greater than 2%
      ^
In file included from optiboot.c:253:0:
optiboot.c: In function 'main':
boot.h:637:5: warning: asm operand 3 probably doesn't match constraints
     __asm__ __volatile__                                   \
     ^
optiboot.c:588:19: note: in expansion of macro 'boot_lock_fuse_bits_get'
           ucr   = boot_lock_fuse_bits_get((uint16_t)ucb1);
                   ^
boot.h:637:5: error: impossible constraint in 'asm'
     __asm__ __volatile__                                   \
     ^
optiboot.c:588:19: note: in expansion of macro 'boot_lock_fuse_bits_get'
           ucr   = boot_lock_fuse_bits_get((uint16_t)ucb1);
                   ^
make: *** [<builtin>: optiboot.o] Error 1

@mcuee
Copy link

mcuee commented Dec 23, 2022

I tried the patch with git HEAD and it can build correctly.

$ git diff
diff --git a/optiboot/bootloaders/optiboot/optiboot.c b/optiboot/bootloaders/optiboot/optiboot.c
index 6d9e0cc..6d19f17 100644
--- a/optiboot/bootloaders/optiboot/optiboot.c
+++ b/optiboot/bootloaders/optiboot/optiboot.c
@@ -979,9 +979,30 @@ int main(void) {
         putch(0x00);
       }
       else {
-        // everything else is ignored
-        getNch(3);
-        putch(0x00);
+    /* STK500 Universal Command */
+    } else if(ch == STK_UNIVERSAL) {
+      #if defined(BIGBOOT)           // real data
+      /* read fuse and lock bits; it takes additional 80 bytes */
+        uint8_t ucb1 = getch();      // universal command byte 1
+        uint8_t ucb2 = getch();      // universal command byte 2
+        getNch(2);                   // discard universal command byte 3-4
+        uint8_t ucr  = 0;            // response
+
+      /* mapping algorithm for function parameter, optimized to save a space:
+        ucb1(0x50), ucb2(0x00) -> GET_LOW_FUSE_BITS     (0x0000)
+        ucb1(0x58), ucb2(0x00) -> GET_LOCK_BITS         (0x0001)
+        ucb1(0x50), ucb2(0x08) -> GET_EXTENDED_FUSE_BITS(0x0002)
+        ucb1(0x58), ucb2(0x08) -> GET_HIGH_FUSE_BITS    (0x0003)
+       */
+        if((ucb1 & ~(0x08)) == 0x50 && (ucb2 & ~(0x08)) == 0x00) {
+          ucb1  = ((ucb1 & 0x08) ? 1 : 0) | (ucb2 >> 2);
+          ucr   = boot_lock_fuse_bits_get((uint16_t)ucb1);
+        }
+       putch(ucr);
+      #else                          // universal command is ignored
+        getNch(4);                   // 4 bytes of data/command
+       putch(0x00);                 // response
+      #endif
       }
 #else
       // UNIVERSAL command is ignored

$ make atmega328 BIGBOOT=1
avr-gcc (AVR_8_bit_GNU_Toolchain_3.6.2_1778) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

BAUD RATE CHECK: Desired: 115200, Real: 117647, UBRRL = 16, Difference=2.1%
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3 -DBIGBOOT=1                -c -o optiboot.o optiboot.c
optiboot.c:429:2: warning: #warning BAUD_RATE off by greater than 2% [-Wcpp]
 #warning BAUD_RATE off by greater than 2%
  ^
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3 -DBIGBOOT=1              -Wl,-Tlink_optiboot.ld -Wl,--relax -nostartfiles -o optiboot_atmega328.elf optiboot.o
avr-size optiboot_atmega328.elf
   text    data     bss     dec     hex filename
    709       0       0     709     2c5 optiboot_atmega328.elf
avr-objcopy -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex optiboot_atmega328.elf optiboot_atmega328.hex
avr-objdump -h -S optiboot_atmega328.elf > optiboot_atmega328.lst
rm optiboot.o

$ make virboot328 BIGBOOT=1
avr-gcc (AVR_8_bit_GNU_Toolchain_3.6.2_1778) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

BAUD RATE CHECK: Desired: 115200, Real: 117647, UBRRL = 16, Difference=2.1%
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3 -DBIGBOOT=1              '-DVIRTUAL_BOOT_PARTITION'   -c -o optiboot.o optiboot.c
optiboot.c:429:2: warning: #warning BAUD_RATE off by greater than 2% [-Wcpp]
 #warning BAUD_RATE off by greater than 2%
  ^
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3 -DBIGBOOT=1              '-DVIRTUAL_BOOT_PARTITION' -Wl,-Tlink_optiboot.ld -Wl,--relax -nostartfiles -o optiboot_virboot328.elf optiboot.o
avr-size optiboot_virboot328.elf
   text    data     bss     dec     hex filename
    846       0       0     846     34e optiboot_virboot328.elf
avr-objcopy -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex optiboot_virboot328.elf optiboot_virboot328.hex
avr-objdump -h -S optiboot_virboot328.elf > optiboot_virboot328.lst
rm optiboot.o


@mcuee
Copy link

mcuee commented Dec 23, 2022

But it does not seem to work.
Note: the board is using ATmega328PB but the booloader is built for ATmega328P.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c arduino -P COM4 -p m328p -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: reading lfuse memory ...
avrdude: writing output file <stdout>
0x0
avrdude: reading hfuse memory ...
avrdude: writing output file <stdout>
0x0
avrdude: reading efuse memory ...
avrdude: writing output file <stdout>
0x0

avrdude done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude -c usbasp -p m328pb -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9516 (probably m328pb)
avrdude: reading lfuse memory ...
avrdude: writing output file <stdout>
0xff
avrdude: reading hfuse memory ...
avrdude: writing output file <stdout>
0xd7
avrdude: reading efuse memory ...
avrdude: writing output file <stdout>
0xf5

avrdude done.  Thank you.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants