Tegraparser_v2 problem

Hello, flashing custom auvidea board jnx45 jetson orin nano with jetpack 6.2, though here custom board does not matter since it is about tegraparser_v2 in bootloader directory. External device is 4TB, luks encryption is enabled, number of sectors is 8001573552 and app enc size is 4093758144512, which is precisely 3813GiB(as supplied to flashing script) minus 419430400 as per script and xml file, here’s calculation from gdb

(gdb) p 3813ULL*1024ULL*1024ULL*1024ULL-419430400ULL
$2 = 4093758144512

attaching xml file as partition.txt.

partition.txt (9.4 KB)

During flash process when tegraflash.py script is invoked in bootloader it uses that xml file(which is itself outputted from flash.sh), to output bin file then from that bin file gpt, using these commands

[   0.0402 ] tegraparser_v2 --pt flash.xml.tmp
...
[   8.2278 ] tegraparser_v2 --generategpt --pt flash.xml.bin

Using these commands on attached xml file will give wrong result for partition size, it will give

gpt_primary_9_0.bin:
partition_id	partition_name			      StartingLba	EndingLba
           1	APP                                 	 3050048	 3869247
           2	APP_ENC                             	 3869248	3704523327
           3	A_kernel                            	      40	  262183
           4	A_kernel-dtb                        	  262184	  263719
           5	A_reserved_on_user                  	  263720	  328487
           6	B_kernel                            	  328488	  590631
           7	B_kernel-dtb                        	  590632	  592167
           8	B_reserved_on_user                  	  592168	  656935
           9	recovery                            	  656936	  820775
          10	recovery-dtb                        	  820776	  821799
          11	esp                                 	  821800	  952871
          12	recovery_alt                        	  952872	 1116711
          13	recovery-dtb_alt                    	 1116712	 1117735
          14	esp_alt                             	 1117736	 1248807
          15	UDA                                 	 1248832	 2068031
          16	reserved                            	 2068032	 3050047
gpt_secondary_9_0.bin:
partition_id	partition_name			      StartingLba	EndingLba
           1	APP                                 	 3050048	 3869247
           2	APP_ENC                             	 3869248	3704523327
           3	A_kernel                            	      40	  262183
           4	A_kernel-dtb                        	  262184	  263719
           5	A_reserved_on_user                  	  263720	  328487
           6	B_kernel                            	  328488	  590631
           7	B_kernel-dtb                        	  590632	  592167
           8	B_reserved_on_user                  	  592168	  656935
           9	recovery                            	  656936	  820775
          10	recovery-dtb                        	  820776	  821799
          11	esp                                 	  821800	  952871
          12	recovery_alt                        	  952872	 1116711
          13	recovery-dtb_alt                    	 1116712	 1117735
          14	esp_alt                             	 1117736	 1248807
          15	UDA                                 	 1248832	 2068031
          16	reserved                            	 2068032	 3050047

For app_enc which is

(gdb) p (3704523327ULL-3869248ULL)*512ULL
$3 = 1894734888448
(gdb) p (1894734888448ULL/1024ULL/1024ULL/1024ULL)
$4 = 1764
(gdb) p (1894734888448ULL/1000ULL/1000ULL/1000ULL)
$5 = 1894

1764 GiB or 1894GB which is what is seen during flashing

Number  Start   End     Size    File system  Name                Flags
 3      20.5kB  134MB   134MB                A_kernel            msftdata
 4      134MB   135MB   786kB                A_kernel-dtb        msftdata
 5      135MB   168MB   33.2MB               A_reserved_on_user  msftdata
 6      168MB   302MB   134MB                B_kernel            msftdata
 7      302MB   303MB   786kB                B_kernel-dtb        msftdata
 8      303MB   336MB   33.2MB               B_reserved_on_user  msftdata
 9      336MB   420MB   83.9MB               recovery            msftdata
10      420MB   421MB   524kB                recovery-dtb        msftdata
11      421MB   488MB   67.1MB  fat32        esp                 boot, esp
12      488MB   572MB   83.9MB               recovery_alt        msftdata
13      572MB   572MB   524kB                recovery-dtb_alt    msftdata
14      572MB   639MB   67.1MB               esp_alt             msftdata
15      639MB   1059MB  419MB                UDA                 msftdata
16      1059MB  1562MB  503MB                reserved            msftdata
 1      1562MB  1981MB  419MB   ext4         APP                 msftdata
 2      1981MB  1897GB  1895GB               APP_ENC             msftdata

Now, the problem with tegraparser_v2 is that in function NvTegraParserPartitionLayout num_sectors is unsigned long so value is truncated, using gdb for the next section it shows what happened

.text:0804A335                 mov     [esp+8Ch+var_88], offset aNumSectors ; "num_sectors"
.text:0804A33D                 mov     eax, [esp+8Ch+var_60]
.text:0804A341                 mov     [esp+8Ch+var_8C], eax
.text:0804A344                 call    NvTegraXmlAttribute
.text:0804A349                 test    eax, eax
.text:0804A34B                 jz      short loc_804A373
.text:0804A34D                 mov     [esp+8Ch+var_84], 0
.text:0804A355                 mov     [esp+8Ch+var_88], 0
.text:0804A35D                 mov     [esp+8Ch+var_8C], eax
.text:0804A360                 call    strtoul
.text:0804A365                 mov     edx, [esp+8Ch+var_64]
.text:0804A369                 mov     [edx+10h], eax
.text:0804A36C                 mov     dword ptr [edx+14h], 0
(gdb) p (const char*)$eax
$19 = 0x80d6de0 "8001573552"
(gdb) ni
0x0804a365 in NvTegraParserPartitionLayout ()
(gdb) p $eax
$20 = -1
(gdb) p (unsigned long)$eax
$21 = 4294967295

Here $19 is before call strtoul so num sectors is that which was supplied in the start of this post, then after stroul it is ULONG_MAX at $21, so then it stores that value in class or struct variable i guess and goes along with it. Maybe i am missing something but it seems that disks with more than 4294967295 sectors do not work properly as main boot disk?

Hi,
It looks like overflow somewhere triggers the issue. It is possible since we don’t test external storage in 4TB. So 2TB works?

Yes 2Tb works

Hi @DaneLLL regarding ‘overflow somewhere’, it overflows at least in one place, namely in NvTegraParserPartitionLayout when parsing num_sectors attribute for that 4TB disk which is 8001573552 sectors, using strtoul, which returns 4294967295 blocks which is ULONG_MAX, since string value does not fit in uint32_t. Which can be seen with gdb in first post.

@DaneLLL hi, is there any way to get patched tegraparser without overflows?

Hi
We are debugging it internally. Will check if we can deliver the binary for Jetpack 6.2.2 r36.5.

Hi,
Please try the attachment.

$ md5sum tegraparser_v2
d8d8351c329cda5c7763a5c4dca74d16  tegraparser_v2

r36_5_TEST_tegraparser_v2.zip (330.0 KB)

@DaneLLL , Hi, thanks, now it works