Skip to contentSkip to conversation

spl: return kernel image header size in os boot

PATCH Superseded Anshul Dalal 3 participants 5 messages Feb 21, 2025 – Feb 25, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spl: return kernel image header size in os boot

During kernel build process the header size is computed including the BSS whereas it's removed when creating the uncompressed image. Therefore the size of the uncompressed image on filesystem will be smaller than the size specifiedin the header.

Therefore it makes sense to return the header size back instead of the file size in falcon boot.

More info:
https://lore.kernel.org/u-boot/[email protected]/

---
 common/spl/spl_ext.c | 4 ++++
 common/spl/spl_fat.c | 3 +++
 2 files changed, 7 insertions(+)
common/spl/spl_ext.c +4 -0
index c5478820a9b..6d8d6544092 100644
@@ -17,6 +17,10 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	ret = ext4fs_read(buf, file_offset, size, &actlen);
 	if (ret)
 		return ret;
+
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actlen;
 }
 
common/spl/spl_fat.c +3 -0
index fce451b7664..ddf85e2cece 100644
@@ -55,6 +55,9 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	if (ret)
 		return ret;
 
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actread;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 37 lines · Anshul Dalal

During kernel build process the header size is computed including the BSS whereas it's removed when creating the uncompressed image. Therefore the size of the uncompressed image on filesystem will be smaller than the size specifiedin the header.

Therefore it makes sense to return the header size back instead of the file size in falcon boot.

More info:
https://lore.kernel.org/u-boot/[email protected]/

---
 common/spl/spl_ext.c | 4 ++++
 common/spl/spl_fat.c | 3 +++
 2 files changed, 7 insertions(+)
common/spl/spl_ext.c +4 -0
index c5478820a9b..6d8d6544092 100644
@@ -17,6 +17,10 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	ret = ext4fs_read(buf, file_offset, size, &actlen);
 	if (ret)
 		return ret;
+
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actlen;
 }
 
common/spl/spl_fat.c +0 -0
index fce451b7664..ddf85e2cece 100644

common/spl/spl_fat.c static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, · line 62

Quoted 8 lines
common/spl/spl_fat.c +3 -0
@@ -55,6 +55,9 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	if (ret)
 		return ret;
 
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actread;
 }

Some image formats include information about the BSS as well, and others do not. But this is just having some parts of SPL return the filesize and not doing some deeper probing. Given that usually problems like this arise from loading contents in to memory too close together and then getting wiped out by the kernel BSS, what's the problem situation you've run in to, where, and with what loaded where in memory?

Signature · 1 line
Tom
#2 lore raw
Quoted 42 lines · Tom Rini
Quoted 40 lines · Anshul Dalal

During kernel build process the header size is computed including the BSS whereas it's removed when creating the uncompressed image. Therefore the size of the uncompressed image on filesystem will be smaller than the size specifiedin the header.

Therefore it makes sense to return the header size back instead of the file size in falcon boot.

More info:
https://lore.kernel.org/u-boot/[email protected]/

---
 common/spl/spl_ext.c | 4 ++++
 common/spl/spl_fat.c | 3 +++
 2 files changed, 7 insertions(+)
common/spl/spl_ext.c +4 -0
index c5478820a9b..6d8d6544092 100644
@@ -17,6 +17,10 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	ret = ext4fs_read(buf, file_offset, size, &actlen);
 	if (ret)
 		return ret;
+
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actlen;
 }
 
common/spl/spl_fat.c +0 -0
index fce451b7664..ddf85e2cece 100644
@@ -55,6 +55,9 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	if (ret)
 		return ret;
 
Quoted 12 lines

common/spl/spl_fat.c static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, · line 62

Quoted 5 lines
common/spl/spl_fat.c +3 -0
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actread;
 }

Some image formats include information about the BSS as well, and others do not. But this is just having some parts of SPL return the filesize and not doing some deeper probing. Given that usually problems like this arise from loading contents in to memory too close together and then getting wiped out by the kernel BSS, what's the problem situation you've run in to, where, and with what loaded where in memory?

The problem is not about the allocation of BSS section but when loading an uncompressed kernel image with the check in _spl_load at include/spl_load.h:95 which fails to -EIO even when the image is loaded properly because it compares the size form the header to the size of the file read from the FS:

	return read < spl_image->size ? -EIO : 0;

This doesn't work for uncompressed kernel image because the size from the header (the parameter ulong size for spl_fit_read) includes BSS which is not part of the final image file. And there is no way of knowing the file size based on the header. Hence why we check for CONFIG_CMD_BOOTI for returning the header size directly.

Although it might be better to have a different function altogether for reading uncompressed image that can be passed to spl_load_init so we are not using spl_**fit**_read to read non fit images.

You can find the bug report here:
https://lore.kernel.org/u-boot/[email protected]/

- Anshul

#3 lore raw
Quoted 70 lines · Anshul Dalal
Quoted 42 lines · Tom Rini
Quoted 40 lines · Anshul Dalal

During kernel build process the header size is computed including the BSS whereas it's removed when creating the uncompressed image. Therefore the size of the uncompressed image on filesystem will be smaller than the size specifiedin the header.

Therefore it makes sense to return the header size back instead of the file size in falcon boot.

More info:
https://lore.kernel.org/u-boot/[email protected]/

---
 common/spl/spl_ext.c | 4 ++++
 common/spl/spl_fat.c | 3 +++
 2 files changed, 7 insertions(+)
common/spl/spl_ext.c +4 -0
index c5478820a9b..6d8d6544092 100644
@@ -17,6 +17,10 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	ret = ext4fs_read(buf, file_offset, size, &actlen);
 	if (ret)
 		return ret;
+
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actlen;
 }
 
common/spl/spl_fat.c +0 -0
index fce451b7664..ddf85e2cece 100644
@@ -55,6 +55,9 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	if (ret)
 		return ret;
 
Quoted 12 lines

common/spl/spl_fat.c static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, · line 62

Quoted 5 lines
common/spl/spl_fat.c +3 -0
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
 	return actread;
 }

Some image formats include information about the BSS as well, and others do not. But this is just having some parts of SPL return the filesize and not doing some deeper probing. Given that usually problems like this arise from loading contents in to memory too close together and then getting wiped out by the kernel BSS, what's the problem situation you've run in to, where, and with what loaded where in memory?

The problem is not about the allocation of BSS section but when loading an uncompressed kernel image with the check in _spl_load at include/spl_load.h:95 which fails to -EIO even when the image is loaded properly because it compares the size form the header to the size of the file read from the FS:

	return read < spl_image->size ? -EIO : 0;

This doesn't work for uncompressed kernel image because the size from the header (the parameter ulong size for spl_fit_read) includes BSS which is not part of the final image file. And there is no way of knowing the file size based on the header. Hence why we check for CONFIG_CMD_BOOTI for returning the header size directly.

Quoted 6 lines

Although it might be better to have a different function altogether for reading uncompressed image that can be passed to spl_load_init so we are not using spl_**fit**_read to read non fit images.

You can find the bug report here:
https://lore.kernel.org/u-boot/[email protected]/

Thanks for explaining. You should include a fixes tag and explain a bit more about the problem being fixed in spl_load, thanks.

Signature · 1 line
Tom
Reviewed-by on v1 1/1
#4 lore raw
In reply to Anshul Dalal
Quoted 37 lines · Anshul Dalal

During kernel build process the header size is computed including the BSS whereas it's removed when creating the uncompressed image. Therefore the size of the uncompressed image on filesystem will be smaller than the size specifiedin the header.

Therefore it makes sense to return the header size back instead of the file size in falcon boot.

More info:
https://lore.kernel.org/u-boot/[email protected]/

---
  common/spl/spl_ext.c | 4 ++++
  common/spl/spl_fat.c | 3 +++
  2 files changed, 7 insertions(+)
common/spl/spl_ext.c +4 -0
index c5478820a9b..6d8d6544092 100644
@@ -17,6 +17,10 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
  	ret = ext4fs_read(buf, file_offset, size, &actlen);
  	if (ret)
  		return ret;
+
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
  	return actlen;
  }
 
common/spl/spl_fat.c +0 -0
index fce451b7664..ddf85e2cece 100644

common/spl/spl_fat.c static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, · line 63

Quoted 9 lines
common/spl/spl_fat.c +3 -0
@@ -55,6 +55,9 @@ static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
  	if (ret)
  		return ret;
 
+	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_CMD_BOOTI))
+		return size;
+
  	return actread;
  }