1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
#include "dialognandsecurity.h"
#include "ui_dialognandsecurity.h"
#include "mainwindow.h"
#include <QDebug>
#include <QByteArray>
#include <QFileInfo>
#include "qhexedit.h"
DialogNandSecurity::DialogNandSecurity(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogNandSecurity)
{
ui->setupUi(this);
setWindowFlags(Qt::Window| Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
}
DialogNandSecurity::~DialogNandSecurity()
{
delete ui;
}
void DialogNandSecurity::on_toolButton_read_clicked()
{
int stCH341 = 0, retval, i;
std::shared_ptr<uint8_t[]> buf(new uint8_t[4096]);
uint8_t curRegister = static_cast<uint8_t>(ui->comboBox_regnum->currentData().toUInt()) + startSector - 1;
qDebug()<<"curregister="<<curRegister;
bool otp;
//READING OTP PAGE
stCH341 = ch341a_spi_init();
usleep(100);
SPI_CONTROLLER_Chip_Select_Low(); //Reading status
SPI_CONTROLLER_Write_One_Byte(0x0f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
retval = SPI_CONTROLLER_Read_NByte(buf.get(),1,SPI_CONTROLLER_SPEED_SINGLE);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
if (stCH341 == 0)
{
if ((buf[0] & 0x40) == 0) //OPT Disabled ?
{
otp = false;
// Enable OTP MODE
SPI_CONTROLLER_Chip_Select_Low(); //Write enable
SPI_CONTROLLER_Write_One_Byte(0x06);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
SPI_CONTROLLER_Chip_Select_Low();
SPI_CONTROLLER_Write_One_Byte(0x1f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
SPI_CONTROLLER_Write_One_Byte(buf[0] | 0x40); //&bf to clear
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
}
else otp = true;
SPI_CONTROLLER_Chip_Select_Low();
SPI_CONTROLLER_Write_One_Byte(0x13);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_One_Byte(curRegister);
//SPI_CONTROLLER_Write_One_Byte(1); //for test ONFI page reading
SPI_CONTROLLER_Chip_Select_High();
usleep(1000);
SPI_CONTROLLER_Chip_Select_Low();
SPI_CONTROLLER_Write_One_Byte(0x03);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_One_Byte(0x00);
retval = SPI_CONTROLLER_Read_NByte(buf.get(),currentSector,SPI_CONTROLLER_SPEED_SINGLE);
SPI_CONTROLLER_Chip_Select_High();
if (retval)
{
QMessageBox::about(this, tr("Error"), tr("Error reading Parameter Page!"));
return;
}
else
{
for (i = 0; i < currentSector; i++)
{
regData[i] = char(buf[i]);
}
hexEdit->setData(regData);
}
if (otp == false) //OPT Disabled
{
// Disable OTP MODE
SPI_CONTROLLER_Chip_Select_Low(); //Reading status
SPI_CONTROLLER_Write_One_Byte(0x0f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
retval = SPI_CONTROLLER_Read_NByte(buf.get(),1,SPI_CONTROLLER_SPEED_SINGLE);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
SPI_CONTROLLER_Chip_Select_Low(); //Write enable
SPI_CONTROLLER_Write_One_Byte(0x06);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
SPI_CONTROLLER_Chip_Select_Low();
SPI_CONTROLLER_Write_One_Byte(0x1f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
SPI_CONTROLLER_Write_One_Byte(buf[0] & 0xbf);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
}
SPI_CONTROLLER_Chip_Select_Low(); //Write disable
SPI_CONTROLLER_Write_One_Byte(0x04);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
ch341a_spi_shutdown();
}
else QMessageBox::about(this, tr("Error"), tr("Programmer CH341a is not connected!"));
}
void DialogNandSecurity::on_toolButton_write_clicked()
{
int stCH341 = 0, retval, i;
std::shared_ptr<uint8_t[]> buf(new uint8_t[4096]);
uint8_t curRegister = static_cast<uint8_t>(ui->comboBox_regnum->currentData().toUInt()) + startSector - 1;
bool otp;
//READING OTP PAGE
stCH341 = ch341a_spi_init();
usleep(100);
SPI_CONTROLLER_Chip_Select_Low(); //Reading status
SPI_CONTROLLER_Write_One_Byte(0x0f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
retval = SPI_CONTROLLER_Read_NByte(buf.get(),1,SPI_CONTROLLER_SPEED_SINGLE);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
if (stCH341 == 0)
{
if ((buf[0] & 0x40) == 0) //OPT Disabled ?
{
otp = false;
// Enable OTP MODE
SPI_CONTROLLER_Chip_Select_Low(); //Write enable
SPI_CONTROLLER_Write_One_Byte(0x06);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
SPI_CONTROLLER_Chip_Select_Low();
SPI_CONTROLLER_Write_One_Byte(0x1f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
SPI_CONTROLLER_Write_One_Byte(buf[0] | 0x40); //&bf to clear
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
}
else otp = true;
SPI_CONTROLLER_Chip_Select_Low(); //Write enable
SPI_CONTROLLER_Write_One_Byte(0x06);
SPI_CONTROLLER_Chip_Select_High();
usleep(200);
regData = hexEdit->data();
for (i = 0; i < static_cast<int>(currentSector); i++)
{
buf[i] = static_cast<uint8_t>(regData[i]);
}
SPI_CONTROLLER_Chip_Select_Low(); //From PC to buffer
SPI_CONTROLLER_Write_One_Byte(0x02);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_NByte( buf.get(), currentSector, SPI_CONTROLLER_SPEED_SINGLE );
SPI_CONTROLLER_Chip_Select_High();
usleep(1000);
SPI_CONTROLLER_Chip_Select_Low(); //From buffer to OTP sector
SPI_CONTROLLER_Write_One_Byte(0x10);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_One_Byte(0x00);
SPI_CONTROLLER_Write_One_Byte(curRegister);
SPI_CONTROLLER_Chip_Select_High();
usleep(200);
if (otp == false) //OPT Disabled
{
// Disable OTP MODE
SPI_CONTROLLER_Chip_Select_Low(); //Reading status
SPI_CONTROLLER_Write_One_Byte(0x0f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
retval = SPI_CONTROLLER_Read_NByte(buf.get(),1,SPI_CONTROLLER_SPEED_SINGLE);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
SPI_CONTROLLER_Chip_Select_Low(); //Write enable
SPI_CONTROLLER_Write_One_Byte(0x06);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
SPI_CONTROLLER_Chip_Select_Low();
SPI_CONTROLLER_Write_One_Byte(0x1f);
SPI_CONTROLLER_Write_One_Byte(0xb0);
SPI_CONTROLLER_Write_One_Byte(buf[0] & 0xbf);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
}
SPI_CONTROLLER_Chip_Select_Low(); //Write disable
SPI_CONTROLLER_Write_One_Byte(0x04);
SPI_CONTROLLER_Chip_Select_High();
usleep(1);
ch341a_spi_shutdown();
}
else QMessageBox::about(this, tr("Error"), tr("Programmer CH341a is not connected!"));
}
void DialogNandSecurity::on_toolButton_open_clicked()
{
QByteArray buf;
QString fileName;
fileName = QFileDialog::getOpenFileName(this,
QString(tr("Open file")),
curPath,
"Data Images (*.bin *.BIN *.rom *.ROM);;All files (*.*)");
QFileInfo info(fileName);
curPath = info.filePath();
QFile file(fileName);
if (info.size() > currentSector)
{
QMessageBox::about(this, tr("Error"), tr("The file size exceeds the security register size."));
return;
}
if (!file.open(QIODevice::ReadOnly))
{
return;
}
buf.resize(static_cast<int>(info.size()));
buf = file.readAll();
regData.replace(0, static_cast<int>(info.size()), buf);
hexEdit->setData(regData);
file.close();
// path must be transfered to mainwindow
}
void DialogNandSecurity::on_toolButton_save_clicked()
{
QString fileName;
fileName = QFileDialog::getSaveFileName(this,
QString(tr("Save file")),
curPath,
"Data Images (*.bin *.BIN);;All files (*.*)");
if (fileName.isEmpty()) return;
QFileInfo info(fileName);
curPath = info.filePath();
if (QString::compare(info.suffix(), "bin", Qt::CaseInsensitive)) fileName = fileName + ".bin";
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::about(this, tr("Error"), tr("Error saving file!"));
return;
}
file.write(hexEdit->data());
file.close();
// path must be transfered to mainwindow
}
void DialogNandSecurity::closeEvent(QCloseEvent* event)
{
emit closeRequestHasArrived();
QWidget::closeEvent(event);
}
void DialogNandSecurity::setSectorSize(uint32_t sectorSize)
{
currentSector = sectorSize;
regData.reserve(4096);
regData.resize(static_cast<int>(currentSector));
regData.fill(char(0xff));
QFont heFont;
heFont = QFont("Monospace", 10);
hexEdit = new QHexEdit(ui->frame);
hexEdit->setGeometry(0, 0, ui->frame->width(), ui->frame->height());
hexEdit->setData(regData);
hexEdit->setHexCaps(true);
hexEdit->setFont(heFont);
hexEdit->setData(regData);
}
void DialogNandSecurity::setAlgorithm(unsigned int currentAlg)
{
uint8_t numSector;
int i;
algSettings algSet[] = {
// id Start_sect Number
{ 0, 0x02, 0x0b },
{ 1, 0x00, 0x03 },
{ 2, 0x00, 0x03 },
{ 3, 0x02, 0x1f },
{ 4, 0x02, 0x0b },
{ 5, 0x02, 0x1d },
{ 6, 0x02, 0x1f },
{ 7, 0x02, 0x1f },
{ 8, 0x02, 0x0b },
{ 9, 0x02, 0x0b },
{ 10, 0x02, 0x1f },
{ 11, 0x00, 0x03 },
{ 12, 0x02, 0x05 },
{ 13, 0x00, 0x03 },
{ 14, 0x02, 0x0b },
{ 15, 0x02, 0x3f },
{ 16, 0x02, 0x3f },
{ 17, 0x00, 0x03 },
{ 18, 0x00, 0x03 },
};
startSector = algSet[currentAlg].secStart;
endSector = algSet[currentAlg].secEnd;
numSector = endSector - startSector + 1;
for (i = 1; i <= numSector; i++)
{
ui->comboBox_regnum->addItem(QString::number(i), i);
}
}
void DialogNandSecurity::setPath(QString lastPath)
{
curPath = lastPath;
}
|