Hex file & flash data

Hello all,
I am trying to understand the hex file and the way it is written in controller's flash memory.
As per now, I have been following
AVR In system programming guide & and Atmega328/8's Datasheet.

  1. I am using Blink.cpp.hex thus generated by compiler(using Arduino only).
  2. I am trying to read the flash memory contents of Controller.

I have made this tentative code to access the flash memory contents. I am pretty sure that the code is working as I have the modified program for reading EEPROM memory as it is verified i.e. it is entering programming mode and I am able to get the fuse bytes and everything else.

void read_flash(int blocks)
{

unsigned char counter=0;
unsigned long address=0;
Serial.print("Reading whole Flash\t");
Serial.println(" ");
byte array[4]={0};

unsigned int address_m=0;
unsigned int address_l=0;
for(address_m=0;address_m<blocks;address_m++)
  {
    address=(unsigned long)address_m*256+address_l;
    Serial.print(address,HEX);
    Serial.print("\t");
    
    for(address_l=0;address_l<256;address_l++)
    {
      array[0]=SPI.transfer(0x28);
      array[1]=SPI.transfer(address_m);
      array[2]=SPI.transfer(address_l);
      array[3]=SPI.transfer(0);
      
        Serial.print(array[3],HEX);
      array[0]=SPI.transfer(0x20);
      array[1]=SPI.transfer(address_m);
      array[2]=SPI.transfer(address_l);
      array[3]=SPI.transfer(0);
      
        Serial.print(array[3],HEX);
        Serial.print(" ");
      counter++;
      if(counter==16)
           {
            Serial.println("");
            counter=0;
            address=(unsigned long)address_m*256+address_l;
            Serial.print(address,HEX);
            Serial.print("\t");
           }
    }
  }

}

I would like to see that exact hex file or the data in it, that I may see using the "hexdump" or "xdd" command on terminal and match that content by reading the Flash memory of MCU.
Attached are the images for what I am getting by now. Kindly help me out as I am not able to endorse the thing.
Plz. do rectify me if I am wrong in the way.

If your intent is to prove that the HEX created is the HEX loaded, it is far easier to do the compare external to the uC. My article gives Windows commands for extracting areas of the chip. These files can then be used to flash a new blank ensuring identical copies.

avr-firmware-duplicator

The format of the file created by the compiler is Intel HEX format. Format docs attached.

Ray

Intel HEX-record Format.pdf (114 KB)

intelhex.pdf (28.5 KB)

Trying to compare IMAGES of hex dumps is difficult. Please post cut&pasted text.

How did you produce "output.bin"? It looks wrong in both the hexdump and your program's output.
The first part of a sketch has the interrupt vectors, which look like a series of "jmp israddress" instructions (each of which is 4 bytes long on a m328.) When I dump Blink.cpp.elf here, I see:

BillW-MacOSX-2<9988> avr-objcopy -O binary -R .eeprom Blink.cpp.elf Blink.bin
BillW-MacOSX-2<9989> hexdump -vx Blink.bin 
0000000    940c    0061    940c    007e    940c    007e    940c    007e
0000010    940c    007e    940c    007e    940c    007e    940c    007e
0000020    940c    007e    940c    007e    940c    007e    940c    007e
0000030    940c    007e    940c    007e    940c    007e    940c    007e
0000040    940c    009a    940c    007e    940c    007e    940c    007e
0000050    940c    007e    940c    007e    940c    007e    940c    007e
0000060    940c    007e    940c    007e    0000    0000    0024    0027

Which corresponds to the code:

   0:   0c 94 61 00     jmp     0xc2    ; 0xc2 <__ctors_end>
*/

#include <Arduino.h>

//Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (*func)()) { return 0; }
   4:   0c 94 7e 00     jmp     0xfc    ; 0xfc <__bad_interrupt>
   8:   0c 94 7e 00     jmp     0xfc    ; 0xfc <__bad_interrupt>
   c:   0c 94 7e 00     jmp     0xfc    ; 0xfc <__bad_interrupt>
  10:   0c 94 7e 00     jmp     0xfc    ; 0xfc <__bad_interrupt>
  14:   0c 94 7e 00     jmp     0xfc    ; 0xfc <__bad_interrupt>

@westfw
Thanks for the concerned reply.

  1. I produced output.bin by using the following command,
    xxd -r -p Blink.cpp.hex output.bin

  2. As I guess, my understanding to the situation of the program burning is quite dicey which I would like to confirm from you.
    My present understanding - The compiler generates HEX file which is simply (without any touch, as pristine as it is generated) is transferred to the MCU's flash and it works there as desired.

When there is this HEX file generated for transferring the program to controller's flash, what is the point of the BIN file, I really don't understand the part of BIN files here. Why to make them ?
Is the main "pertinent" code is the bin file or the hex file or what?

What is exact difference between the HEX thus generated and the bin file we may generate (as it is not generated in the /tmp/build** folder so I guess it serves really no use.(correct me If I am wrong).

I am confused here, and would really appreciate you bearing me in this topic.

If I do, as you have shown me in your above post, then the output is as follows,

beyond@beyond-HP-Pavilion:~/Downloads/Burner_AVR/build_blink_led$ avr-objcopy -O binary -R .eeprom Blink.cpp.elf Blink.bin
beyond@beyond-HP-Pavilion:~/Downloads/Burner_AVR/build_blink_led$ hexdump -vx Blink.bin
0000000 940c 0061 940c 0073 940c 0073 940c 0073
0000010 940c 0073 940c 0073 940c 0073 940c 0073
0000020 940c 0073 940c 0073 940c 0073 940c 0073
0000030 940c 0073 940c 0073 940c 0073 940c 0073
0000040 940c 0118 940c 0073 940c 0073 940c 0073
0000050 940c 0073 940c 0073 940c 0073 940c 0073
0000060 940c 0073 940c 0073 0000 0000 0024 0027

which I really don't know how to convert back to CODE form, which you showed above.(how how how? so want to know)

mrburnette:
If your intent is to prove that the HEX created is the HEX loaded, it is far easier to do the compare external to the uC. My article gives Windows commands for extracting areas of the chip. These files can then be used to flash a new blank ensuring identical copies.

Thanks for your help @mrburnette
I am able to exactly copy the flash's content from one controller to another using the above documents.
What I am not able to do, is to match the content up and understand it both ways.

Also, the output at Serial port for reading the Flash of controller is :

Reading whole Flash 
0 C03F C04E C04D C04C C04B C04A C049 C048 C047 C0C8 C045 C044 C043 C042 C041 C040 
F C03F C03E C03D 00 00 037 034 031 00 00 038 035 032 00 00 036 
1F 033 030 44 44 44 44 22 22 22 33 33 33 21 84 2010 8040 
2F 21 84 2010 21 84 2010 00 00 00 00 30 54 00 00 00 00 
3F 2411 BE1F E5CF E0D4 BFDE BFCD E010 E6A0 E0B0 C01 921D 36A9 7B1 F7E1 D137 C141 
4F CFAF E08D E061 D023 958 E08D E061 D043 E664 E070 E080 E090 D0E4 E08D E060 D03B 
5F E664 E070 E080 E090 D0DC 958 3084 F039 3085 F049 3083 F451 B58F 778F C02 B58F 
6F 7D8F BD8F 958 B585 7D8F BD85 958 E090 1FC 5AE8 4FFF 9124 1FC 5BEC 4FFF 91E4 
7F 23EE F0C1 E0F0 FEE 1FFF 5DEA 4FFF 9185 9194 1DC 2366 F441 B79F 94F8 918C 9520 
8F 2382 938C BF9F 958 B79F 94F8 918C 2B82 938C BF9F 958 93F 931F 93DF 93CF 92F 
9F B7CD B7DE 2F28 E030 1F9 59E4 4FFF 9184 1F9 5AE8 4FFF 9114 5B2C 4F3F 1F9 914 
AF 230 F0D1 2388 F019 8369 DFB0 8169 2FE0 E0F0 FEE 1FFF 5DE0 4FFF 9185 9194 1DC 
BF B79F 94F8 2366 F421 918C 9510 2381 C02 918C 2B81 938C BF9F 90F 91CF 91DF 911F 
CF 91F 958 921F 92F B6F 92F 2411 932F 933F 938F 939F 93AF 93BF 9180 064 9190 
DF 065 91A0 066 91B0 067 9130 068 961 1DA1 1DB1 2F23 5F2D 372D F020 572D 961 
EF 1DA1 1DB1 9320 068 9380 064 9390 065 93A0 066 93B0 067 9180 060 9190 061 
FF 200 91A0 062 91B0 063 961 1DA1 1DB1 9380 060 9390 061 93A0 062 93B0 063 91BF 
10F 91AF 919F 918F 913F 912F 90F BEF 90F 901F 9518 B79F 94F8 9120 060 9130 061 
11F 9140 062 9150 063 B782 B68 FE0 C06 3F8F F021 5F2F 4F3F 4F4F 4F5F BF9F 2F54 
12F 2F43 2F32 2722 F28 1D31 1D41 1D51 E082 F22 1F33 1F44 1F55 958A F7D1 1B9 1CA 
13F 958 92EF 92FF 93F 931F 93CF 93DF 17B 18C DFD0 1EB C0E DFCD 1B6C B7D E083 
14F 3E68 778 F038 948 8E1 8F1 91 911 51C8 4FDC 14E1 4F1 51 511 F769 91DF 
15F 91CF 911F 91F 90FF 90EF 958 9478 B783 6082 BF83 B783 6081 BF83 B789 6081 BF89 
16F BC1E B58E 6082 BD8E B58E 6081 BD8E B58F 6081 BD8F B585 6084 BD85 B585 6480 BD85 
17F 9A32 9A31 9A30 9A37 B81A 958 93CF 93DF DFDD DEC7 E0C0 E0D0 DEC8 9720 F3E9 DE70 
18F CFFB 94F8 CFFF BD8E B58E 6081 BD8E B58F 6081 BD8F B585 6084 BD85 B585 6480 BD85 
19F 89B7 912C E081 E090 8C5 C02 F88 1F99 94A F7E2 2B28 932C E081 E090 911F 958 
1AF 1FB 5CE0 4FFF 8120 8131 5F2F 4F3F 732F 7030 1DB 5BAE 4FBF 914D 915C 9711 1724 
1BF 735 F039 81A0 81B1 FA6 1FB7 938C 8331 8320 958 958 921F 92F B6F 92F 2411 
1CF 932F 933F 934F 935F 936F 937F 938F 939F 93AF 93BF 93EF 93FF B18C E76A E070 DFD0 
1DF 91FF 91EF 91BF 91AF 919F 918F 917F 916F 915F 914F 913F 912F 90F BEF 90F 901F 
1EF 9518 E082 E091 DF1F 970 F09 DFD3 958 921F 92F B6F 92F 2411 932F 933F 935F 
1FF 300 936F 937F 938F 939F 93AF 93BF 93EF 93FF 9120 0FE 9130 0FF 9180 10 9190 11 
20F 1728 739 F411 9855 C014 91E0 10 91F0 11 54E2 4FFF 8120 9180 10 9190 11 
21F 961 E460 E070 D1AC 9390 11 9380 10 B92C 91FF 91EF 91BF 91AF 919F 918F 917F 
22F 916F 915F 913F 912F 90F BEF 90F 901F 9518 92DF 92EF 92FF 93F 931F 93CF 93DF 
23F 1EC 17A 18B 24DD 94D3 3040 EE81 758 E080 768 E080 778 F449 24DD 89EC 89FD 
24F 8210 E860 E874 E18E E090 C0F 89EC 89FD E081 E090 8CE C02 F88 1F99 94A F7E2 
25F 8380 E060 E079 E38D E090 1A8 197 D17B 5021 4030 4040 4050 9556 9547 9537 9527 
26F E180 3020 738 F010 20DD F6B9 89E8 89F9 8330 89EA 89FB 8320 89EE 89FF 8140 E081 
27F E090 19C 8CA C02 F22 1F33 94A F7E2 2B42 8340 89EE 89FF 8140 19C 8CB C02 
28F F22 1F33 94A F7E2 2B42 8340 89EE 89FF 8140 19C 8CC C02 F22 1F33 94A F7E2 
29F 2B42 8340 89EE 89FF 8120 8CD C02 F88 1F99 94A F7E2 9580 2382 8380 91DF 91CF 
2AF 911F 91F 90FF 90EF 90DF 958 9210 15 9210 14 EE88 E093 E0A0 E0B0 9380 16 
2BF 9390 17 93A0 18 93B0 19 E0E4 E0F1 E684 E090 9392 9382 E78A E090 8795 8784 
2CF EB8E E090 8797 8786 E480 E090 8B91 8B80 E289 E090 8B93 8B82 E28B E090 8B95 8B84 
2DF E28A E090 8B97 8B86 E28C E090 8F91 8F80 E084 8F82 E083 8F83 E087 8F84 E085 8F85 
2EF E081 8F86 958 93CF 93DF DDFD DD43 EFC1 E0D1 DD61 9720 F3E9 DEF4 CFFB 92CF 92DF 
2FF 400 92EF 92FF 93F 931F 93CF 93DF 16C 17B 18A E0C0 E0D0 C0F 1D7 916D 17D 1D6 
30F 91ED 91FC 901 81F0 2DE0 1C6 959 FC8 1FD9 501 4010 151 511 F771 1CE 91DF 
31F 91CF 911F 91F 90FF 90EF 90DF 90CF 958 1DB 90D 200 F7E9 1AD 5041 4050 1B46 
32F B57 1DC 91ED 91FC 802 81F3 2DE0 959 958 1DC 91ED 91FC 901 81F0 2DE0 959 
33F 958 93F 931F 93CF 93DF 1EC E06D DFF1 18C 1CE E06A DFED F8 1F19 1C8 91DF 
34F 91CF 911F 91F 958 928F 929F 92AF 92BF 92CF 92DF 92EF 92FF 93F 931F 93DF 93CF 
35F B7CD B7DE 97A1 B6F 94F8 BFDE BEF BFCD 16C 2F4 2FE5 1CB 2F12 A219 3022 F48 
36F E01A E221 2EE2 2CF1 EEC 1EFD 2E81 2499 24AA 24BB C03 2F2 2FE3 1CA 2F60 2F7E 
37F 1A5 194 D060 9F12 2D80 2411 1B8 948 8E1 8F1 30A F414 5D0 C01 5C9 1F7 
38F 830 1521 531 541 551 F729 1C6 1B7 DF8F 96A1 B6F 94F8 BFDE BEF BFCD 91CF 
39F 91DF 911F 91F 90FF 90EF 90DF 90CF 90BF 90AF 909F 908F 958 1521 531 F449 1DC 
3AF 91ED 91FC 901 81F0 2DE0 2F64 959 958 DF9B 958 93F 931F 93CF 93DF 1EC 19A 
3BF 2F46 E050 E060 E070 DFE7 18C 1CE DF79 F8 1F19 1C8 91DF 91CF 911F 91F 958 
3CF FB97 2E9 267 D0A FD77 D04 D02E D06 200 F41A 9570 9561 4F7F 958 F7F6 9590 
3DF 9581 4F9F 958 E2A1 2E1A 1BAA 1BBB 1FD C0D 1FAA 1FBB 1FEE 1FFF 17A2 7B3 7E4 
3EF 7F5 F020 1BA2 BB3 BE4 BF5 1F66 1F77 1F88 1F99 941A F769 9560 9570 9580 9590 
3FF 500 19B 1AC 1BD 1CF 958 1BAA 1BBB E151 C07 1FAA 1FBB 17A6 7B7 F010 1BA6 BB7 
40F 1F88 1F99 955A F7A9 9580 9590 1BC 1CD 958 FEE 1FFF 905 91F4 2DE0 949 99E1 
41F CFFE BB9F BB8E 9AE0 2799 B38D 958 94F8 CFFF 00 00 178 2FE 113 142 126 
42F 164 1F99 955A F7A9 9580 9590 1BC 1CD 958 FEE 1FFF 905 91F4 2DE0 949 99E1 
43F FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
44F FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
45F FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
46F FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
47F FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
48F FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
49F FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
4AF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF

My present understanding - The compiler generates HEX file which is simply (without any touch, as pristine as it is generated) is transferred to the MCU's flash and it works there as desired.

No, that's wrong. The .hex file is a textual representation of the binary it represents, looking like:

:100000000C9461000C947E000C947E000C947E0095
:100010000C947E000C947E000C947E000C947E0068
:100020000C947E000C947E000C947E000C947E0058
:100030000C947E000C947E000C947E000C947E0048
:100040000C949A000C947E000C947E000C947E001C

Each line includes the delimiter, a length byte, a record type, an address word, and a checksum in addition to the data (which is in printable hex.) It is NOT AT ALL a "binary" file.

In the case of Arduino, the .hex file is converted to binary during the "upload" process, by the avrdude program used for uploading. Other programming schemes may upload the .hex format directly, and have it converted to binary by the bootloader in the destination processor.

AFAIK, Arduino doesn't use .BIN files anywhere. The compiler produces .o files, the linker combines those into .elf files, objcopy converts/extracts to .hex files, and avrdude uploads from the .hex file. The only reason to create a .bin file would be if you wanted to look at the instructions in binary/hex, all by themselves. All of the other formats include "extra" values that are not actually programmed into the AVR chip.

Convert back to code form with objdump: avr-objdump -SC Blink.cpp.elf
It's best to use the .elf file, which contains type and symbol information that objdump uses. You can run objdump on a .hex or .bin file, but you have to tell it which sub-type of AVR your are using, and you won't get symbolic names.
(Something like avr-objdump -D -bbinary -mavr *.bin )

Your read_flash() looks basically correct...

and Atmega328/8's Datasheet.

Is the target chip that you're looking at an ATmega8, rather than an ATmega328? That will make a big difference in what the binary looks like, mostly because the m8 has 2-byte vectors instead of 4-byte vectors. In fact, if I compile Blink for an ATmega8, I get code that looks a lot like what your program printed:

hexdump -vx Blink8.bin 
0000000    c03f    c059    c058    c057    c056    c055    c054    c053
0000010    c052    c067    c050    c04f    c04e    c04d    c04c    c04b
0000020    c04a    c049    c048    0000    0000    0037    0034    0031
0000030    0000    0000    0038    0035    0032    0000    0000    0036
0000040    0033    0030    0404    0404    0404    0404    0202    0202
0000050    0202    0303    0303    0303    0201    0804    2010    8040
0000060    0201    0804    2010    0201    0804    2010    0000    0000
0000070    0000    0000    0300    0504    0000    0000    0000    0000

While the Atmega8 and ATmega328 are both AVRs, they are members of different "sub-families", which means that there will be some significant differences in the code that the compiler produces. You can't just run binary code compiled for one AVR on some other AVR; the compiler thinks that there are something like six different sub-families (not that you can find Atmel documentation that divides them up so neatly.)

My sketch below reads a .hex file.

As westfw says there is more in each line that the pure data. There are addresses, lengths, transaction type, and also a sumcheck at the end of each line.

In particular, the functions readHexFile and processLine.


@saurabhshandilya: Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" button above the posting area (It looks like a scroll with < > inside it).

westfw:
No, that's wrong. The .hex file is a textual representation of the binary it represents, looking like:

:100000000C9461000C947E000C947E000C947E0095

:100010000C947E000C947E000C947E000C947E0068
:100020000C947E000C947E000C947E000C947E0058
:100030000C947E000C947E000C947E000C947E0048
:100040000C949A000C947E000C947E000C947E001C




Each line includes the delimiter, a length byte, a record type, an address word, and a checksum in addition to the data (which is in printable hex.) It is NOT AT ALL a "binary" file.

In the case of Arduino, the .hex file is converted to binary during the "upload" process, by the avrdude program used for uploading. Other programming schemes may upload the .hex format directly, and have it converted to binary by the bootloader in the destination processor.

Copy that captain!
I read about HEX file and Bin files and it all makes sense to me now. "Epiphany of truth" to me :)
But still after understanding all this, I am now scrutinizing the avrdude's code to search this operation there.
I just want to get total crystal clear on this.


AFAIK, Arduino doesn't use .BIN files anywhere. The compiler produces .o files, the linker combines those into .elf files, objcopy converts/extracts to .hex files, and avrdude uploads from the .hex file. The only reason to create a .bin file would be if you wanted to look at the instructions in binary/hex, all by themselves. All of the other formats include "extra" values that are not actually programmed into the AVR chip.

Yes sir, totally got it.

Convert back to code form with objdump: avr-objdump -SC Blink.cpp.elf
It's best to use the .elf file, which contains type and symbol information that objdump uses. You can run objdump on a .hex or .bin file, but you have to tell it which sub-type of AVR your are using, and you won't get symbolic names.
(Something like avr-objdump -D -bbinary -mavr *.bin )

Your read_flash() looks basically correct...


Is the target chip that you're looking at an ATmega8, rather than an ATmega328? That will make a big difference in what the binary looks like, mostly because the m8 has 2-byte vectors instead of 4-byte vectors. In fact, if I compile Blink for an ATmega8, I get code that looks a lot like what your program printed:


[tt]hexdump -vx Blink8.bin
0000000    c03f    c059    c058    c057    c056    c055    c054    c053
0000010    c052    c067    c050    c04f    c04e    c04d    c04c    c04b
0000020    c04a    c049    c048    0000    0000    0037    0034    0031
0000030    0000    0000    0038    0035    0032    0000    0000    0036
0000040    0033    0030    0404    0404    0404    0404    0202    0202
0000050    0202    0303    0303    0303    0201    0804    2010    8040
0000060    0201    0804    2010    0201    0804    2010    0000    0000
0000070    0000    0000    0300    0504    0000    0000    0000    0000





[tt]

Yes, sir! that is indeed an blunder mistake of mine to not to make it clear explicitly that which Blink hex I am talking of 328's or atmega 8's.
I was working with both and thats why I and alongside made you confused too.
Sorry for that.


While the Atmega8 and ATmega328 are both AVRs, they are members of different "sub-families", which means that there will be some significant differences in the code that the compiler produces. You can't just run binary code compiled for one AVR on some other AVR; the compiler thinks that there are something like six different sub-families (not that you can find Atmel documentation that divides them up so neatly.)

Yes, sir. I do know the BIG difference between them, been working on them for quite a time and am embarrassed for creating the confusion among them.

[/tt]

I indeed got the same result by doing the "avr-objcopy" to generate the .bin file and then viewing the .bin file's hexdump.
But one thing is yet to my concern.
that is, if I read the flash as I did in #post5, I got this data which is quite "more" than the hexdump is showing me.
As I know that before writing a new program to flash, avrdude performs a erase cycle so that makes rest of the flash as FF. (Correct me in case I am wrong)
=> the Part in the code of #post5 where I am started getting these FF, that is the part which is not affected by programming the Blink program.
The full hexdump of Blink.bin file gives me this:

beyond@beyond-HP-Pavilion:~/Downloads/Burner_AVR/build_opti_8$ hexdump -vx Blink.bin 
0000000    c03f    c04e    c04d    c04c    c04b    c04a    c049    c048
0000010    c047    c0c8    c045    c044    c043    c042    c041    c040
0000020    c03f    c03e    c03d    0000    0000    0037    0034    0031
0000030    0000    0000    0038    0035    0032    0000    0000    0036
0000040    0033    0030    0404    0404    0404    0404    0202    0202
0000050    0202    0303    0303    0303    0201    0804    2010    8040
0000060    0201    0804    2010    0201    0804    2010    0000    0000
0000070    0000    0000    0300    0504    0000    0000    0000    0000
0000080    2411    be1f    e5cf    e0d4    bfde    bfcd    e010    e6a0
0000090    e0b0    c001    921d    36a9    07b1    f7e1    d137    c141
00000a0    cfaf    e08d    e061    d023    9508    e08d    e061    d043
00000b0    ee68    e073    e080    e090    d0e4    e08d    e060    d03b
00000c0    ee68    e073    e080    e090    d0dc    9508    3084    f039
00000d0    3085    f049    3083    f451    b58f    778f    c002    b58f
00000e0    7d8f    bd8f    9508    b585    7d8f    bd85    9508    e090
00000f0    01fc    5ae8    4fff    9124    01fc    5bec    4fff    91e4
0000100    23ee    f0c1    e0f0    0fee    1fff    5dea    4fff    9185
0000110    9194    01dc    2366    f441    b79f    94f8    918c    9520
0000120    2382    938c    bf9f    9508    b79f    94f8    918c    2b82
0000130    938c    bf9f    9508    930f    931f    93df    93cf    920f
0000140    b7cd    b7de    2f28    e030    01f9    59e4    4fff    9184
0000150    01f9    5ae8    4fff    9114    5b2c    4f3f    01f9    9104
0000160    2300    f0d1    2388    f019    8369    dfb0    8169    2fe0
0000170    e0f0    0fee    1fff    5de0    4fff    9185    9194    01dc
0000180    b79f    94f8    2366    f421    918c    9510    2381    c002
0000190    918c    2b81    938c    bf9f    900f    91cf    91df    911f
00001a0    910f    9508    921f    920f    b60f    920f    2411    932f
00001b0    933f    938f    939f    93af    93bf    9180    0064    9190
00001c0    0065    91a0    0066    91b0    0067    9130    0068    9601
00001d0    1da1    1db1    2f23    5f2d    372d    f020    572d    9601
00001e0    1da1    1db1    9320    0068    9380    0064    9390    0065
00001f0    93a0    0066    93b0    0067    9180    0060    9190    0061
0000200    91a0    0062    91b0    0063    9601    1da1    1db1    9380
0000210    0060    9390    0061    93a0    0062    93b0    0063    91bf
0000220    91af    919f    918f    913f    912f    900f    be0f    900f
0000230    901f    9518    b79f    94f8    9120    0060    9130    0061
0000240    9140    0062    9150    0063    b782    b608    fe00    c006
0000250    3f8f    f021    5f2f    4f3f    4f4f    4f5f    bf9f    2f54
0000260    2f43    2f32    2722    0f28    1d31    1d41    1d51    e082
0000270    0f22    1f33    1f44    1f55    958a    f7d1    01b9    01ca
0000280    9508    92ef    92ff    930f    931f    93cf    93df    017b
0000290    018c    dfd0    01eb    c00e    dfcd    1b6c    0b7d    e083
00002a0    3e68    0778    f038    9408    08e1    08f1    0901    0911
00002b0    51c8    4fdc    14e1    04f1    0501    0511    f769    91df
00002c0    91cf    911f    910f    90ff    90ef    9508    9478    b783
00002d0    6082    bf83    b783    6081    bf83    b789    6081    bf89
00002e0    bc1e    b58e    6082    bd8e    b58e    6081    bd8e    b58f
00002f0    6081    bd8f    b585    6084    bd85    b585    6480    bd85
0000300    9a32    9a31    9a30    9a37    b81a    9508    93cf    93df
0000310    dfdd    dec7    e0c0    e0d0    dec8    9720    f3e9    de70
0000320    cffb    94f8    cfff                                        
0000326

On the other hand, The read performed by me on the Flash give this on serial :
Reading whole Flash

C03F C04E C04D C04C C04B C04A C049 C048 
C047 C0C8 C045 C044 C043 C042 C041 C040 
C03F C03E C03D 00 00 037 034 031 
00 00 038 035 032 00 00 036 
033 030 44 44 44 44 22 22 
22 33 33 33 21 84 2010 8040 
21 84 2010 21 84 2010 00 00 
00 00 30 54 00 00 00 00 
2411 BE1F E5CF E0D4 BFDE BFCD E010 E6A0 
E0B0 C01 921D 36A9 7B1 F7E1 D137 C141 
CFAF E08D E061 D023 958 E08D E061 D043 
E664 E070 E080 E090 D0E4 E08D E060 D03B 
E664 E070 E080 E090 D0DC 958 3084 F039 
3085 F049 3083 F451 B58F 778F C02 B58F 
7D8F BD8F 958 B585 7D8F BD85 958 E090 
1FC 5AE8 4FFF 9124 1FC 5BEC 4FFF 91E4 
23EE F0C1 E0F0 FEE 1FFF 5DEA 4FFF 9185 
9194 1DC 2366 F441 B79F 94F8 918C 9520 
2382 938C BF9F 958 B79F 94F8 918C 2B82 
938C BF9F 958 93F 931F 93DF 93CF 92F 
B7CD B7DE 2F28 E030 1F9 59E4 4FFF 9184 
1F9 5AE8 4FFF 9114 5B2C 4F3F 1F9 914 
230 F0D1 2388 F019 8369 DFB0 8169 2FE0 
E0F0 FEE 1FFF 5DE0 4FFF 9185 9194 1DC 
B79F 94F8 2366 F421 918C 9510 2381 C02 
918C 2B81 938C BF9F 90F 91CF 91DF 911F 
91F 958 921F 92F B6F 92F 2411 932F 
933F 938F 939F 93AF 93BF 9180 064 9190 
065 91A0 066 91B0 067 9130 068 961 
1DA1 1DB1 2F23 5F2D 372D F020 572D 961 
1DA1 1DB1 9320 068 9380 064 9390 065 
93A0 066 93B0 067 9180 060 9190 061 
91A0 062 91B0 063 961 1DA1 1DB1 9380 
060 9390 061 93A0 062 93B0 063 91BF 
91AF 919F 918F 913F 912F 90F BEF 90F 
901F 9518 B79F 94F8 9120 060 9130 061 
9140 062 9150 063 B782 B68 FE0 C06 
3F8F F021 5F2F 4F3F 4F4F 4F5F BF9F 2F54 
2F43 2F32 2722 F28 1D31 1D41 1D51 E082 
F22 1F33 1F44 1F55 958A F7D1 1B9 1CA 
958 92EF 92FF 93F 931F 93CF 93DF 17B 
18C DFD0 1EB C0E DFCD 1B6C B7D E083 
3E68 778 F038 948 8E1 8F1 91 911 
51C8 4FDC 14E1 4F1 51 511 F769 91DF 
91CF 911F 91F 90FF 90EF 958 9478 B783 
6082 BF83 B783 6081 BF83 B789 6081 BF89 
BC1E B58E 6082 BD8E B58E 6081 BD8E B58F 
6081 BD8F B585 6084 BD85 B585 6480 BD85 
9A32 9A31 9A30 9A37 B81A 958 93CF 93DF 
DFDD DEC7 E0C0 E0D0 DEC8 9720 F3E9 DE70 
CFFB 94F8 CFFF BD8E B58E 6081 BD8E B58F 
----------------------------------------------------------------
6081 BD8F B585 6084 BD85 B585 6480 BD85 
89B7 912C E081 E090 8C5 C02 F88 1F99 
94A F7E2 2B28 932C E081 E090 911F 958 
1FB 5CE0 4FFF 8120 8131 5F2F 4F3F 732F 
7030 1DB 5BAE 4FBF 914D 915C 9711 1724 
735 F039 81A0 81B1 FA6 1FB7 938C 8331 
8320 958 958 921F 92F B6F 92F 2411 
932F 933F 934F 935F 936F 937F 938F 939F 
93AF 93BF 93EF 93FF B18C E76A E070 DFD0 
91FF 91EF 91BF 91AF 919F 918F 917F 916F 
915F 914F 913F 912F 90F BEF 90F 901F 
9518 E082 E091 DF1F 970 F09 DFD3 958 
921F 92F B6F 92F 2411 932F 933F 935F 
936F 937F 938F 939F 93AF 93BF 93EF 93FF 
9120 0FE 9130 0FF 9180 10 9190 11 
1728 739 F411 9855 C014 91E0 10 91F0 
11 54E2 4FFF 8120 9180 10 9190 11 
961 E460 E070 D1AC 9390 11 9380 10 
B92C 91FF 91EF 91BF 91AF 919F 918F 917F 
916F 915F 913F 912F 90F BEF 90F 901F 
9518 92DF 92EF 92FF 93F 931F 93CF 93DF 
1EC 17A 18B 24DD 94D3 3040 EE81 758 
E080 768 E080 778 F449 24DD 89EC 89FD 
8210 E860 E874 E18E E090 C0F 89EC 89FD 
E081 E090 8CE C02 F88 1F99 94A F7E2 
8380 E060 E079 E38D E090 1A8 197 D17B 
5021 4030 4040 4050 9556 9547 9537 9527 
E180 3020 738 F010 20DD F6B9 89E8 89F9 
8330 89EA 89FB 8320 89EE 89FF 8140 E081 
E090 19C 8CA C02 F22 1F33 94A F7E2 
2B42 8340 89EE 89FF 8140 19C 8CB C02 
F22 1F33 94A F7E2 2B42 8340 89EE 89FF 
8140 19C 8CC C02 F22 1F33 94A F7E2 
2B42 8340 89EE 89FF 8120 8CD C02 F88 
1F99 94A F7E2 9580 2382 8380 91DF 91CF 
911F 91F 90FF 90EF 90DF 958 9210 15 
9210 14 EE88 E093 E0A0 E0B0 9380 16 
9390 17 93A0 18 93B0 19 E0E4 E0F1 
E684 E090 9392 9382 E78A E090 8795 8784 
EB8E E090 8797 8786 E480 E090 8B91 8B80 
E289 E090 8B93 8B82 E28B E090 8B95 8B84 
E28A E090 8B97 8B86 E28C E090 8F91 8F80 
E084 8F82 E083 8F83 E087 8F84 E085 8F85 
E081 8F86 958 93CF 93DF DDFD DD43 EFC1 
E0D1 DD61 9720 F3E9 DEF4 CFFB 92CF 92DF 
92EF 92FF 93F 931F 93CF 93DF 16C 17B 
18A E0C0 E0D0 C0F 1D7 916D 17D 1D6 
91ED 91FC 901 81F0 2DE0 1C6 959 FC8 
1FD9 501 4010 151 511 F771 1CE 91DF 
91CF 911F 91F 90FF 90EF 90DF 90CF 958 
1DB 90D 200 F7E9 1AD 5041 4050 1B46 
B57 1DC 91ED 91FC 802 81F3 2DE0 959 
958 1DC 91ED 91FC 901 81F0 2DE0 959 
958 93F 931F 93CF 93DF 1EC E06D DFF1 
18C 1CE E06A DFED F8 1F19 1C8 91DF 
91CF 911F 91F 958 928F 929F 92AF 92BF 
92CF 92DF 92EF 92FF 93F 931F 93DF 93CF 
B7CD B7DE 97A1 B6F 94F8 BFDE BEF BFCD 
16C 2F4 2FE5 1CB 2F12 A219 3022 F48 
E01A E221 2EE2 2CF1 EEC 1EFD 2E81 2499 
24AA 24BB C03 2F2 2FE3 1CA 2F60 2F7E 
1A5 194 D060 9F12 2D80 2411 1B8 948 
8E1 8F1 30A F414 5D0 C01 5C9 1F7 
830 1521 531 541 551 F729 1C6 1B7 
DF8F 96A1 B6F 94F8 BFDE BEF BFCD 91CF 
91DF 911F 91F 90FF 90EF 90DF 90CF 90BF 
90AF 909F 908F 958 1521 531 F449 1DC 
91ED 91FC 901 81F0 2DE0 2F64 959 958 
DF9B 958 93F 931F 93CF 93DF 1EC 19A 
2F46 E050 E060 E070 DFE7 18C 1CE DF79 
F8 1F19 1C8 91DF 91CF 911F 91F 958 
FB97 2E9 267 D0A FD77 D04 D02E D06 
200 F41A 9570 9561 4F7F 958 F7F6 9590 
9581 4F9F 958 E2A1 2E1A 1BAA 1BBB 1FD 
C0D 1FAA 1FBB 1FEE 1FFF 17A2 7B3 7E4 
7F5 F020 1BA2 BB3 BE4 BF5 1F66 1F77 
1F88 1F99 941A F769 9560 9570 9580 9590 
19B 1AC 1BD 1CF 958 1BAA 1BBB E151 
C07 1FAA 1FBB 17A6 7B7 F010 1BA6 BB7 
1F88 1F99 955A F7A9 9580 9590 1BC 1CD 
958 FEE 1FFF 905 91F4 2DE0 949 99E1 
CFFE BB9F BB8E 9AE0 2799 B38D 958 94F8 
CFFF 00 00 178 2FE 113 142 126 
164 1F99 955A F7A9 9580 9590 1BC 1CD 
958 FEE 1FFF 905 91F4 2DE0 949 99E1 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF

I have put a ------ line to seperate them.
So, I would like to ask what this extra data is that I am yet not understanding.

If you're using the bootloader, it does NOT erase memory before programming a new sketch, so if you program a short sketch like blink, it can leave pieces of the previous sketch sitting in flash memory. There's no way to tell how long the actual code is once it's been burned into flash, as far as I know.

I am now scrutinizing the avrdude's code

avrdude is probably pretty hard to understand, because it "abstracts" things in order to support a large number of different programmers and (maybe) file formats. It would not surprise me if it read in the entire input file into some "internal format" (linked lists of binary regions, for example), and then wrote out to the programmer in a format described by the configuration file...

@westfw :

westfw:
If you're using the bootloader, it does NOT erase memory before programming a new sketch, so if you program a short sketch like blink, it can leave pieces of the previous sketch sitting in flash memory. There's no way to tell how long the actual code is once it's been burned into flash, as far as I know.

yes sir, I indeed got it confirmed by writing a big program first and then a small program and then reading the flash and I am able to see the rest of the whole big program written over in the flash of controller.
I did the operation on Atmega328 and Atmega8 both, but one thing still caught my interest.

After everytime one burns a program to Atmega using Arduino uploader, it adds some extra bytes to the end i.e. (to the data I get by viewing the hexdump).
The data from reading the Flash and hexdump matches exactly but a few bytes are added up when I read from the flash.
As you said and also it is visible that previous program is not erased first but the new program is loaded instead.
So the controller must have something to know that "this time", to which address my program is,(as my current program to get executed is a lot more smaller than the past program).

These number of bytes added are different for different programs.
My question:

  1. Is this automatically generated by the uploader to know where my current program ends? If yes, then how ?
  2. How will it affect if I don't add these bytes up in the end, If I write my own program to the flash, guess in that case I have to erase my flash first.

For example:

Atmega 328 blink read flash after erase

Get signature program
Entering programming mode
0	0	53	0
Successfully entered programming mode
Obtaining signatures
signature address 1 is 	1E
signature address 2 is 	95
signature address 3 is 	F
Obtaining Fuses
Hfuse is	DE
Lfuse is	FF
Extended fuses are 	FD
Reading whole Flash	
94C 061 94C 073 94C 073 94C 073 
94C 073 94C 073 94C 073 94C 073 
94C 073 94C 073 94C 073 94C 073 
94C 073 94C 073 94C 073 94C 073 
94C 118 94C 073 94C 073 94C 073 
94C 073 94C 073 94C 073 94C 073 
94C 073 94C 073 00 00 024 027 
02A 00 00 025 028 02B 00 00 
023 026 029 44 44 44 44 22 
22 22 33 33 33 21 84 2010 
8040 21 84 2010 21 84 2010 00 
70 20 01 30 64 00 00 00 
00 2411 BE1F EFCF E0D8 BFDE BFCD E011 
E0A0 E0B1 C01 921D 30A9 7B1 F7E1 94E 
1E8 94C 1F7 94C 00 E08D E061 94E 
0BC 958 E08D E061 94E 0E0 EE68 E073 
E080 E090 94E 186 E08D E060 94E 0E0 
EE68 E073 E080 E090 94E 186 958 3083 
F071 3084 F428 3081 F0A1 3082 F521 C014 
3086 F0B1 3087 F0D1 3084 F4E9 C04 9180 
080 778F C03 9180 080 7D8F 9380 080 
958 B584 778F C02 B584 7D8F BD84 958 
9180 0B0 778F 9380 0B0 958 9180 0B0 
7D8F 9380 0B0 958 E090 1FC 56E6 4FFF 
9124 1FC 57EA 4FFF 91E4 23EE F0C1 E0F0 
FEE 1FFF 59E8 4FFF 9185 9194 1DC 2366 
F441 B79F 94F8 918C 9520 2382 938C BF9F 
958 B79F 94F8 918C 2B82 938C BF9F 958 
93F 931F 93DF 93CF 92F B7CD B7DE 2F28 
E030 1F9 55E2 4FFF 9184 1F9 56E6 4FFF 
9114 572A 4F3F 1F9 914 230 F0D9 2388 
F021 8369 94E 08F 8169 2FE0 E0F0 FEE 
1FFF 58EE 4FFF 9185 9194 1DC B79F 94F8 
2366 F421 918C 9510 2381 C02 918C 2B81 
938C BF9F 90F 91CF 91DF 911F 91F 958 
921F 92F B6F 92F 2411 932F 933F 938F 
939F 93AF 93BF 9180 14 9190 15 91A0 
16 91B0 17 9130 18 961 1DA1 1DB1 
2F23 5F2D 372D F020 572D 961 1DA1 1DB1 
9320 18 9380 14 9390 15 93A0 16 
93B0 17 9180 10 9190 11 91A0 12 
91B0 13 961 1DA1 1DB1 9380 10 9390 
11 93A0 12 93B0 13 91BF 91AF 919F 
918F 913F 912F 90F BEF 90F 901F 9518 
B79F 94F8 9120 10 9130 11 9140 12 
9150 13 B586 9BA8 C06 3F8F F021 5F2F 
4F3F 4F4F 4F5F BF9F 2F54 2F43 2F32 2722 
F28 1D31 1D41 1D51 E082 F22 1F33 1F44 
1F55 958A F7D1 1B9 1CA 958 92EF 92FF 
93F 931F 93CF 93DF 17B 18C 94E 160 
1EB C0F 94E 160 1B6C B7D E083 3E68 
778 F038 948 8E1 8F1 91 911 51C8 
4FDC 14E1 4F1 51 511 F761 91DF 91CF 
911F 91F 90FF 90EF 958 9478 B584 6082 
BD84 B584 6081 BD84 B585 6082 BD85 B585 
6081 BD85 E6EE E0F0 8180 6081 8380 E8E1 
E0F0 8210 8180 6082 8380 8180 6081 8380 
E8E0 E0F0 8180 6081 8380 EBE1 E0F0 8180 
6084 8380 EBE0 E0F0 8180 6081 8380 E7EA 
E0F0 8180 6084 8380 8180 6082 8380 8180 
6081 8380 8180 6880 8380 9210 0C1 958 
93CF 93DF 94E 1AD 94E 075 E0C0 E0D0 
94E 07A 9720 F3E1 94E 00 CFF9 94F8 
CFFF BD85 E6EE E0F0 8180 6081 8380 E8E1 
--------------------------------------- 7  extra pair of bytes
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF 
FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF

//The hexdump of bin file

beyond@beyond-HP-Pavilion:~/Downloads/Burner_AVR/blink_328$ hexdump -vx Blink.bin 
0000000    940c    0061    940c    0073    940c    0073    940c    0073
0000010    940c    0073    940c    0073    940c    0073    940c    0073
0000020    940c    0073    940c    0073    940c    0073    940c    0073
0000030    940c    0073    940c    0073    940c    0073    940c    0073
0000040    940c    0118    940c    0073    940c    0073    940c    0073
0000050    940c    0073    940c    0073    940c    0073    940c    0073
0000060    940c    0073    940c    0073    0000    0000    0024    0027
0000070    002a    0000    0000    0025    0028    002b    0000    0000
0000080    0023    0026    0029    0404    0404    0404    0404    0202
0000090    0202    0202    0303    0303    0303    0201    0804    2010
00000a0    8040    0201    0804    2010    0201    0804    2010    0000
00000b0    0700    0200    0001    0300    0604    0000    0000    0000
00000c0    0000    2411    be1f    efcf    e0d8    bfde    bfcd    e011
00000d0    e0a0    e0b1    c001    921d    30a9    07b1    f7e1    940e
00000e0    01e8    940c    01f7    940c    0000    e08d    e061    940e
00000f0    00bc    9508    e08d    e061    940e    00e0    ee68    e073
0000100    e080    e090    940e    0186    e08d    e060    940e    00e0
0000110    ee68    e073    e080    e090    940e    0186    9508    3083
0000120    f071    3084    f428    3081    f0a1    3082    f521    c014
0000130    3086    f0b1    3087    f0d1    3084    f4e9    c004    9180
0000140    0080    778f    c003    9180    0080    7d8f    9380    0080
0000150    9508    b584    778f    c002    b584    7d8f    bd84    9508
0000160    9180    00b0    778f    9380    00b0    9508    9180    00b0
0000170    7d8f    9380    00b0    9508    e090    01fc    56e6    4fff
0000180    9124    01fc    57ea    4fff    91e4    23ee    f0c1    e0f0
0000190    0fee    1fff    59e8    4fff    9185    9194    01dc    2366
00001a0    f441    b79f    94f8    918c    9520    2382    938c    bf9f
00001b0    9508    b79f    94f8    918c    2b82    938c    bf9f    9508
00001c0    930f    931f    93df    93cf    920f    b7cd    b7de    2f28
00001d0    e030    01f9    55e2    4fff    9184    01f9    56e6    4fff
00001e0    9114    572a    4f3f    01f9    9104    2300    f0d9    2388
00001f0    f021    8369    940e    008f    8169    2fe0    e0f0    0fee
0000200    1fff    58ee    4fff    9185    9194    01dc    b79f    94f8
0000210    2366    f421    918c    9510    2381    c002    918c    2b81
0000220    938c    bf9f    900f    91cf    91df    911f    910f    9508
0000230    921f    920f    b60f    920f    2411    932f    933f    938f
0000240    939f    93af    93bf    9180    0104    9190    0105    91a0
0000250    0106    91b0    0107    9130    0108    9601    1da1    1db1
0000260    2f23    5f2d    372d    f020    572d    9601    1da1    1db1
0000270    9320    0108    9380    0104    9390    0105    93a0    0106
0000280    93b0    0107    9180    0100    9190    0101    91a0    0102
0000290    91b0    0103    9601    1da1    1db1    9380    0100    9390
00002a0    0101    93a0    0102    93b0    0103    91bf    91af    919f
00002b0    918f    913f    912f    900f    be0f    900f    901f    9518
00002c0    b79f    94f8    9120    0100    9130    0101    9140    0102
00002d0    9150    0103    b586    9ba8    c006    3f8f    f021    5f2f
00002e0    4f3f    4f4f    4f5f    bf9f    2f54    2f43    2f32    2722
00002f0    0f28    1d31    1d41    1d51    e082    0f22    1f33    1f44
0000300    1f55    958a    f7d1    01b9    01ca    9508    92ef    92ff
0000310    930f    931f    93cf    93df    017b    018c    940e    0160
0000320    01eb    c00f    940e    0160    1b6c    0b7d    e083    3e68
0000330    0778    f038    9408    08e1    08f1    0901    0911    51c8
0000340    4fdc    14e1    04f1    0501    0511    f761    91df    91cf
0000350    911f    910f    90ff    90ef    9508    9478    b584    6082
0000360    bd84    b584    6081    bd84    b585    6082    bd85    b585
0000370    6081    bd85    e6ee    e0f0    8180    6081    8380    e8e1
0000380    e0f0    8210    8180    6082    8380    8180    6081    8380
0000390    e8e0    e0f0    8180    6081    8380    ebe1    e0f0    8180
00003a0    6084    8380    ebe0    e0f0    8180    6081    8380    e7ea
00003b0    e0f0    8180    6084    8380    8180    6082    8380    8180
00003c0    6081    8380    8180    6880    8380    9210    00c1    9508
00003d0    93cf    93df    940e    01ad    940e    0075    e0c0    e0d0
00003e0    940e    007a    9720    f3e1    940e    0000    cff9    94f8
00003f0    cfff                                                        
00003f2
beyond@beyond-HP-Pavilion:~/Downloads/Burner_AVR/blink_328$

And these extra bytes are same if I upload some BIG program first and then the Blink program, so to me, it appears as a kind of End of Current Program indicator

link=msg=1972459:
My sketch below reads a .hex file.
arduino_sketches/Atmega_Hex_Uploader/Atmega_Hex_Uploader.ino at master · nickgammon/arduino_sketches · GitHub

As westfw says there is more in each line that the pure data. There are addresses, lengths, transaction type, and also a sumcheck at the end of each line.

In particular, the functions readHexFile and processLine.


Ahan, got it sir.
can't be more exact.

After everytime one burns a program to Atmega using Arduino uploader, it adds some extra bytes to the end i.e. (to the data I get by viewing the hexdump).

You ask good questions! This is probably a cooperative bug/mis-feature of the bootloader and the avrdude program.

The ATMEGA chips self-program their chips in "pages" of flash memory; each time you want to change a flash location, you have to write the whole page of memory. The bootloader receives a page of data from avrdude, and then programs that page. Which is swell, except for the LAST page of an upload, which may not be "full."
Avrdude, in its attempts to be efficient, does not send a full page when only some of the bytes are valid, nor does it fill the page with 0xFF bytes that would look like "unprogrammed" data. The bootloader, since it is so tiny, does not pre-fill the page with 0xFF. So when a non-full last page is written, the end of that page probably still contains the data from the previous page programmed, as in your example:

BD84 B584 6081 BD84 B585 6082 BD85 B585 
6081 [color=maroon]BD85 E6EE E0F0 8180 6081 8380 E8E1 [/color]
-- Page boundary --
E0F0 8210 8180 6082 8380 8180 6081 8380 
E8E0 E0F0 8180 6081 8380 EBE1 E0F0 8180 
6084 8380 EBE0 E0F0 8180 6081 8380 E7EA 
E0F0 8180 6084 8380 8180 6082 8380 8180 
6081 8380 8180 6880 8380 9210 0C1 958 
93CF 93DF 94E 1AD 94E 075 E0C0 E0D0 
94E 07A 9720 F3E1 94E 00 CFF9 94F8 
CFFF [color=maroon]BD85 E6EE E0F0 8180 6081 8380 E8E1 [/color]
--------------------------------------- 7  extra pair of bytes

This has actually caused bugs:
https://code.google.com/p/optiboot/issues/detail?id=59

@westfw - True that sir,
I totally get what you are telling me. But what I am trying to convey is that even if I do these:

  1. "complete erase" of atmega328 flash
  2. Re put the bootloader.
  3. read the flash and I get all as FF
  4. Burn the Led blink for 328 again
  5. Read the flash
    it is still showing those extra bytes, the data is not just upto CFFF.
    Since there is no such data from the last page, so even if the bootloader or the dude doesn't send extra FF in order to complete the page, it should show the previous FF, which I see is not the case.

So now two questions:

  1. How are these extra bytes coming even if I confirmed it before writing that the whole flash is FF.
  2. How the controller know that upto which bytes is my relevant program and
    aren't these extra bytes going to affect my controller. I mean I really don't know Machine language and architecture things much, but I think it probably "may" cause undesired behavior at times.

The extra bytes are from the next-to-last PAGE of the CURRENT sketch, not the previous sketch.
Say that the page size is 8, and Blink looks like
11111111
22222222
33333333
4444
And memory starts off as all F's.
The bootloader loads up the first page "11111111" and programs it to flash.
Repeat for 2s and 3s. Now avrdude sends the last page of instructions "4444", but the buffer the bootloader uses still contains 3's, so the last page programmed will be "44443333" Since the 3s are beyond the program memory actually used by the sketch, they shouldn't affect anything.