sceNand functions and dumping the flash

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

sceNand functions and dumping the flash

Post by moonlight »

i've gladly seen that the sceNand functions have been recently added to the PSPSDK and also a good example dumping the IPL.

I've tried to create a program that dumps all the nand to a single binary file... but it doesn't work as expected.

Code: Select all

nblocks = sceNandGetTotalBlocks();
	ppb = sceNandGetPagesPerBlock();
	cbBlock = ppb * sceNandGetPageSize();

	block = (u8 *)malloc(cbBlock);

	for &#40;i = 0; i < nblocks; i++&#41;
	&#123;
		sceNandLock&#40;0&#41;;

		if &#40;!sceNandIsBadBlock&#40;i&#41;&#41;
		&#123;

			memset&#40;block, 0, cbBlock&#41;;
			goodb++;
		
			if &#40;sceNandReadBlockWithRetry&#40;i*ppb, block, NULL&#41; < 0&#41;
			&#123;
				printf&#40;"Error reading block #%d\n", i&#41;;
				sceNandUnlock&#40;&#41;;
				continue;
			&#125;

			writtenb++;
			printf&#40;"Dumping block #%d\n", i&#41;;
			sceIoWrite&#40;fd, block, cbBlock&#41;;
		&#125;
		
		else
		&#123;
			printf&#40;"bad block &#58;#%d\n", i&#41;;
		&#125;

		sceNandUnlock&#40;&#41;;
	&#125;

	printf&#40;"Done. good blocks&#58; %d, written&#58; %d\n", goodb, writtenb&#41;;

At the end, goodb (good blocks) = 64 and writtenb (written blocks) = 61, and the file is less than 1 MB when it should be exactly 32 MB if it had written the 2048 blocks.

What are exactly bad blocks? and why 3 of the good blocks are not read correctly? does the kernel block access to read them?
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

update your sdk....mrbrown (i think) added a sample that dumps the flash using scenand interface :)
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

groepaz wrote:update your sdk....mrbrown (i think) added a sample that dumps the flash using scenand interface :)
yes, i have it and i have read his code before making the mine. but it only dumps the ipl.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

ah ok i see :)

about the bad blocks.... you maybe want to read up on NAND flash (get a datasheet on a simelar flash). in a nutshell, NAND flashes use EDC coding and "backup" blocks simelar to HDs do. i dont know how exactly this is handled by the sceNand driver though, i got distracted from reversing that one :=P
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Re: sceNand functions and dumping the flash

Post by mrbrown »

moonlight wrote:

Code: Select all

nblocks = sceNandGetTotalBlocks&#40;&#41;;
	ppb = sceNandGetPagesPerBlock&#40;&#41;;
	cbBlock = ppb * sceNandGetPageSize&#40;&#41;;

	block = &#40;u8 *&#41;malloc&#40;cbBlock&#41;;

	for &#40;i = 0; i < nblocks; i++&#41;
	&#123;
		sceNandLock&#40;0&#41;;

		if &#40;!sceNandIsBadBlock&#40;i&#41;&#41;
		&#123;
The problem is the argument to sceNandIsBadBlock(). All flash addresses are given in page offsets, so you have to pass the physical page number instead of the block number (you're already doing this in your call to sceNandReadBlockWithRetry()).
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Re: sceNand functions and dumping the flash

Post by moonlight »

mrbrown wrote: The problem is the argument to sceNandIsBadBlock(). All flash addresses are given in page offsets, so you have to pass the physical page number instead of the block number (you're already doing this in your call to sceNandReadBlockWithRetry()).
ok. i've solved that, what a stupid mistake of mine. now the result is:

good blocks: 2048 (i have the flash in perfect state... at the moment :P )
written blocks: 1947

The dump, ~30.4 MB. i wanted to fully backup every byte, every bit of the flash to a file, but i don't know why those 2048-1947 = 101 blocks cannot be read.

maybe there is another way?
lS[UMD/2kdlSU]
Posts: 8
Joined: Wed Oct 26, 2005 10:08 pm
Location: Shiga, Japan
Contact:

Post by lS[UMD/2kdlSU] »

I've written a small program that reads flash with sceNandReadPages function.
As a result,
- Some of the page (could not read with sceNandReadBlockWithRetry) could read with sceNandReadPages function.
- Others (could read with sceNandReadBlockWithRetry) could not read...

What is the difference?

# Sorry for my bad English..
lS[UMD/2kdlSU] - now working as 67...
----------------------------------------------
site: jap | eng
jap blog: here
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

lS[UMD/2kdlSU] wrote:What is the difference?
None, because sceNandReadBlockWithRetry() just calls sceNandReadPages() with a page count of 32. Check to make sure if you're reading a whole block with sceNandReadBlock/ReadPages that your page count is exactly 32, and that your PPN starts on a block boundary (divisible by 32).

Another restriction is that if you read pages within a block, the most pages you can read is 32 - (PPN & 0x1f).
Post Reply