From c3462a3d24999d583bc151637b17b3b31eae54ea Mon Sep 17 00:00:00 2001 From: "giantpune@gmail.com" Date: Fri, 14 Jan 2011 17:48:38 +0000 Subject: [PATCH] * allow reading of old nand.bin for creating a new one --- ohneschwanzenegger/newnandbin.cpp | 156 +++++++++++++++++++++++++----- ohneschwanzenegger/newnandbin.h | 8 +- ohneschwanzenegger/newnandbin.ui | 19 +++- 3 files changed, 154 insertions(+), 29 deletions(-) diff --git a/ohneschwanzenegger/newnandbin.cpp b/ohneschwanzenegger/newnandbin.cpp index 2cb1990..7f58fe9 100644 --- a/ohneschwanzenegger/newnandbin.cpp +++ b/ohneschwanzenegger/newnandbin.cpp @@ -9,7 +9,7 @@ NewNandBin::NewNandBin( QWidget *parent, QList badBlocks ) : QDialog(pa foreach( quint16 block, badBlocks ) { QString txt = QString( "%1" ).arg( block ); - if( !ui->listWidget_badBlocks->findItems( txt, Qt::MatchExactly ).isEmpty() ) + if( ui->listWidget_badBlocks->findItems( txt, Qt::MatchExactly ).isEmpty() ) ui->listWidget_badBlocks->addItem( txt ); } } @@ -79,28 +79,9 @@ void NewNandBin::on_pushButton_bb_rm_clicked() } } -//ok clicked -void NewNandBin::on_buttonBox_accepted() +bool NewNandBin::CreateDefaultEntries() { - if( ui->lineEdit_keys->text().isEmpty() || ui->lineEdit_boot->text().isEmpty() || ui->lineEdit_dest->text().isEmpty() ) - { - QMessageBox::warning( this, tr( "Error" ), tr( "Required feilds are empty" ) ); - return; - } - QByteArray keys = ReadFile( ui->lineEdit_keys->text() ); - QByteArray boots = ReadFile( ui->lineEdit_boot->text() ); - if( keys.size() != 0x400 || boots.size() != 0x108000 ) - { - QMessageBox::warning( this, tr( "Error" ), tr( "The keys or boot1/2 is not correct" ) ); - return; - } - if( !nand.CreateNew( ui->lineEdit_dest->text(), keys, boots, BadBlocks() ) ) - { - qDebug() << "error creating nand.bin"; - return; - } - //qDebug() << "created nand, trying to add default entries"; - if( !nand.CreateEntry( "/sys", 0, 0, NAND_DIR, NAND_RW, NAND_RW, 0 ) + if( !nand.CreateEntry( "/sys", 0, 0, NAND_DIR, NAND_RW, NAND_RW, 0 ) || !nand.CreateEntry( "/ticket", 0, 0, NAND_DIR, NAND_RW, NAND_RW, 0 ) || !nand.CreateEntry( "/title", 0, 0, NAND_DIR, NAND_RW, NAND_RW, NAND_READ ) || !nand.CreateEntry( "/shared1", 0, 0, NAND_DIR, NAND_RW, NAND_RW, 0 ) @@ -111,19 +92,61 @@ void NewNandBin::on_buttonBox_accepted() || !nand.WriteMetaData() ) { qWarning() << "NewNandBin::on_buttonBox_accepted -> error creating directories in the new nand"; - return; - } + QMessageBox::warning( this, tr( "Error" ), \ + tr( "Can't create base folders in the new nand." ) ); + return false; + } //add cert.sys quint16 handle = nand.CreateEntry( "/sys/cert.sys", 0, 0, NAND_FILE, NAND_RW, NAND_RW, NAND_READ ); if( !handle || !nand.SetData( handle, QByteArray( (const char*)&certs_dat, CERTS_DAT_SIZE ) ) ) { qWarning() << "NewNandBin::on_buttonBox_accepted -> error creating cert in the new nand"; - return; + QMessageBox::warning( this, tr( "Error" ), \ + tr( "Can't create cert.sys folders in the new nand." ) ); + return false; } //commit changes to metadata if( !nand.WriteMetaData() ) { qWarning() << "NewNandBin::on_buttonBox_accepted -> error writing metadata"; + QMessageBox::warning( this, tr( "Error" ), \ + tr( "Can't write metadata in the new nand." ) ); + return false; + } + return true; +} + +//ok clicked +void NewNandBin::on_buttonBox_accepted() +{ + if( ui->lineEdit_keys->text().isEmpty() || ui->lineEdit_boot->text().isEmpty() || ui->lineEdit_dest->text().isEmpty() ) + { + QMessageBox::warning( this, tr( "Error" ), tr( "Required feilds are empty" ) ); + return; + } + if( keys.isEmpty() ) + keys = ReadFile( ui->lineEdit_keys->text() ); + if( boots.isEmpty() ) + boots = ReadFile( ui->lineEdit_boot->text() ); + if( keys.size() != 0x400 || boots.size() != 0x108000 ) + { + QMessageBox::warning( this, tr( "Error" ), tr( "The keys or boot1/2 is not correct" ) ); + keys.clear(); + boots.clear(); + return; + } + if( !nand.CreateNew( ui->lineEdit_dest->text(), keys, boots, BadBlocks() ) ) + { + qDebug() << "error creating nand.bin"; + keys.clear(); + boots.clear(); + return; + } + //qDebug() << "created nand, trying to add default entries"; + if( !CreateDefaultEntries() ) + { + keys.clear(); + boots.clear(); return; } @@ -171,3 +194,86 @@ void NewNandBin::on_pushButton_badBlockFile_clicked() ui->listWidget_badBlocks->addItem( line ); } } + +//read info from existing nand.bin +void NewNandBin::on_pushButton_oldNand_clicked() +{ + QString f = QFileDialog::getOpenFileName( this, tr( "Select Old nand.bin" ), dir ); + if( f.isEmpty() ) + return; + + + QFileInfo fi( f ); + QFile file( fi.absoluteFilePath() ); + if( !file.exists() || !file.open( QIODevice::ReadOnly ) ) + { + QMessageBox::warning( this, tr( "Error" ), \ + tr( "Can't open %1!" ).arg( fi.absoluteFilePath() ) ); + return; + } + ui->listWidget_badBlocks->clear(); + + switch( fi.size() ) + { + case 0x21000000:// w/ ecc, keys in different file + { + boots = file.read( 0x108000 ); + //file.seek(); + keys = ReadFile( fi.absoluteDir().absoluteFilePath( "keys.bin" ) ); + + } + break; + case 0x21000400:// w/ ecc, keys at end of nand dump + { + boots = file.read( 0x108000 ); + file.seek( 0x21000000 ); + keys = file.read( 0x400 ); + //keys = ReadFile( fi.absoluteDir().absoluteFilePath( "keys.bin" ) ); + + } + break; + default://unsupported for this + QMessageBox::warning( this, tr( "Error" ), tr( "I need a nand dump with ecc to create a new nand from.
Accepted sizes are 0x21000000 and 0x21000400." ) ); + file.close(); + return; + break; + } + file.close(); + + //create nandBin object to get the list of bad blocks + NandBin old( this ); + if( !old.SetPath( fi.absoluteFilePath() ) || !old.InitNand() ) + { + QMessageBox::warning( this, tr( "Error" ), \ + tr( "Error reading %1." ).arg( fi.absoluteFilePath() ) ); + keys.clear(); + boots.clear(); + return; + } + QList clusters = old.GetFats(); + QList badBlacks; + if( !clusters.size() == 0x8000 ) + { + QMessageBox::warning( this, tr( "Error" ), \ + tr( "Expected 0x8000 clusters from the nand, but got %1 instead!" ).arg( clusters.size(), 0, 16 ), QMessageBox::Ok ); + keys.clear(); + boots.clear(); + return; + } + for( quint16 i = 0; i < 0x8000; i += 8 )//first cluster of each block. + { + //qDebug() << hex << i << clusters.at( i ); + if( clusters.at( i ) == 0xFFFD ) + { + quint16 block = ( i / 8 ); + badBlacks << block; + QString txt = QString( "%1" ).arg( block ); + qDebug() << "bad cluster" << hex << i << block << txt; + //if( ui->listWidget_badBlocks->findItems( txt, Qt::MatchExactly ).isEmpty() )//just in case, but this should always be true + ui->listWidget_badBlocks->addItem( txt ); + } + } + ui->lineEdit_boot->setText( tr( "" ) ); + ui->lineEdit_keys->setText( tr( "" ) ); + +} diff --git a/ohneschwanzenegger/newnandbin.h b/ohneschwanzenegger/newnandbin.h index 156898c..185b730 100644 --- a/ohneschwanzenegger/newnandbin.h +++ b/ohneschwanzenegger/newnandbin.h @@ -27,8 +27,14 @@ private: QString ret; QString dir; + QByteArray boots; + QByteArray keys; + + bool CreateDefaultEntries(); + private slots: - void on_pushButton_badBlockFile_clicked(); + void on_pushButton_oldNand_clicked(); + void on_pushButton_badBlockFile_clicked(); void on_buttonBox_accepted(); void on_pushButton_bb_rm_clicked(); void on_pushButton_bb_add_clicked(); diff --git a/ohneschwanzenegger/newnandbin.ui b/ohneschwanzenegger/newnandbin.ui index 0e41f83..eef0947 100644 --- a/ohneschwanzenegger/newnandbin.ui +++ b/ohneschwanzenegger/newnandbin.ui @@ -14,7 +14,7 @@ New Nand - + @@ -178,7 +178,7 @@ - + Qt::Horizontal @@ -191,7 +191,7 @@ - + Qt::Horizontal @@ -201,6 +201,19 @@ + + + + + 150 + 69 + + + + Existing Nand... + + +