| 1 | // Copyright (C) 2014 Sacha Refshauge |
| 2 | |
| 3 | // This program is free software: you can redistribute it and/or modify |
| 4 | // it under the terms of the GNU General Public License as published by |
| 5 | // the Free Software Foundation, version 3.0. |
| 6 | |
| 7 | // This program is distributed in the hope that it will be useful, |
| 8 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | // GNU General Public License 3.0 for more details. |
| 11 | |
| 12 | // A copy of the GPL 3.0 should have been included with the program. |
| 13 | // If not, see http://www.gnu.org/licenses/ |
| 14 | |
| 15 | // Official GIT repository and contact information can be found at |
| 16 | // http://github.com/xsacha/Sachesi |
| 17 | |
| 18 | #include "ifs.h" |
| 19 | |
| 20 | namespace FS { |
| 21 | |
| 22 | binode IFS::createBNode(int offset, qint64 startPos) { |
| 23 | QNXStream stream(_file); |
| 24 | _file->seek(startPos + offset); |
| 25 | binode ind; |
| 26 | stream >> ind.mode >> ind.size >> ind.time; |
| 27 | |
| 28 | ind.name = QString(_file->readLine(QNX6_MAX_CHARS)); |
| 29 | if (ind.name == "") |
| 30 | ind.name = "."; |
| 31 | ind.offset = _file->pos() - startPos; |
| 32 | |
| 33 | return ind; |
| 34 | } |
| 35 | |
| 36 | QString IFS::generateName(QString imageExt) { |
| 37 | _file->seek(_offset + 0x40); |
| 38 | QString builder = QString(_file->readLine(16)); // ec_agent, developer or username |
| 39 | if (builder == "ec_agent") |
| 40 | builder = "prod"; |
| 41 | else if (builder == "developer") |
| 42 | builder = "trunk"; |
| 43 | |
| 44 | _file->seek(_offset + 0x50); |
| 45 | QStringList buildDateList = QString(_file->readLine(16)).split(' '); // Mmm dd yyyy |
| 46 | buildDateList.swap(0,1); // Swap day and month |
| 47 | QString buildDate = buildDateList.join(""); |
| 48 | |
| 49 | _file->seek(_offset + 0xAC); |
| 50 | QNXStream stream(_file); |
| 51 | READ_TMP(short, build); |
| 52 | READ_TMP(quint8, majorminor); |
| 53 | READ_TMP(quint8, os); |
| 54 | QString ifs_ver = QString("%1.%2.%3.%4") |
| 55 | .arg(os) |
| 56 | .arg(majorminor >> 3) |
| 57 | .arg(majorminor & 0x7) |
| 58 | .arg(build); |
| 59 | |
| 60 | QString name = QString("boot-%1-%2-%3") |
| 61 | .arg(ifs_ver) |
| 62 | .arg(builder) |
| 63 | .arg(buildDate); |
| 64 | if (imageExt.isEmpty()) |
| 65 | return uniqueDir(name); |
| 66 | |
| 67 | return uniqueFile(name + imageExt); |
| 68 | } |
| 69 | |
| 70 | void IFS::extractDir(int offset, int numNodes, QString basedir, qint64 startPos) |
| 71 | { |
| 72 | |
| 73 | Q_UNUSED(offset); |
| 74 | Q_UNUSED(numNodes); |
| 75 | Q_UNUSED(basedir); |
| 76 | Q_UNUSED(startPos); |
| 77 | // TODO: Nodes seem to fall apart. This must be whole-image compressed? |
| 78 | /* |
| 79 | QNXStream stream(_file); |
| 80 | QDir mainDir(basedir); |
| 81 | for (int i = 0; i < numNodes; i++) { |
| 82 | binode node = createBNode(offset + (i * 0x20), startPos); |
| 83 | qDebug() << QString::number(node.mode,16) << node.name << QString::number(node.offset,16); |
| 84 | if (node.mode & QCFM_IS_DIRECTORY) { |
| 85 | qDebug() << "Is directory"; |
| 86 | //extractRCFSDir(node.offset, node.size / 0x20, node.path_to + "/" + node.name, startPos); |
| 87 | } else { |
| 88 | if (node.mode & QCFM_IS_SYMLINK) { |
| 89 | _file->seek(startPos + node.offset); |
| 90 | qDebug() << "Symlink: " << node.name << " -> " << _file->readLine(QNX6_MAX_CHARS); |
| 91 | } else if (node.mode & QCFM_IS_COMPRESSED) { |
| 92 | qDebug() << "Is compressed file"; |
| 93 | } else { |
| 94 | qDebug() << "Is regular file"; |
| 95 | } |
| 96 | } |
| 97 | }*/ |
| 98 | } |
| 99 | |
| 100 | bool IFS::createContents() { |
| 101 | QNXStream stream(_file); |
| 102 | _file->seek(_offset + 1); |
| 103 | qint8 type; |
| 104 | qint32 boot_size, startup_size; |
| 105 | stream >> type; |
| 106 | if (type == 3) { // Qualcomm |
| 107 | _file->seek(_offset + 0x1020); |
| 108 | // boot @ 0 with boot_size; |
| 109 | stream >> boot_size; |
| 110 | boot_size &= 0xfffff; |
| 111 | } |
| 112 | else { // 1 // OMAP |
| 113 | // No boot.bin |
| 114 | boot_size = 0x808; |
| 115 | } |
| 116 | |
| 117 | _file->seek(_offset + boot_size); |
| 118 | // Make sure there is a startup header |
| 119 | if (_file->read(4) != QByteArray::fromHex("EB7EFF00")) { |
| 120 | // It may be offset by 0x1000 |
| 121 | boot_size += 0x1000; |
| 122 | _file->seek(_offset + boot_size); |
| 123 | if (_file->read(4) != QByteArray::fromHex("EB7EFF00")) { |
| 124 | return false; // Not a valid IFS image |
| 125 | } |
| 126 | } |
| 127 | _file->seek(_offset + boot_size + 0x20); |
| 128 | // startup @ boot_size + 0x100 with startup_size - 0x100 |
| 129 | stream >> startup_size; |
| 130 | // imagefs @ boot_size + startup_size |
| 131 | //extractBootDir(0xC, 1, _path, _offset + boot_size + startup_size); |
| 132 | |
| 133 | |
| 134 | // Temporarily dump the components until a full extraction is available |
| 135 | QDir(_path).mkpath("."); |
| 136 | // -- Dump boot.bin -- |
| 137 | if (boot_size > 0x1100) // Does it have a boot.bin? Some speciality images don't and start at 0x1008 |
| 138 | QFileSystem::writeFile("boot.bin", _offset + 0x1100, boot_size - 0x1100); |
| 139 | // -- Dump startup.bin |
| 140 | QFileSystem::writeFile("startup.bin", _offset + boot_size + 0x100, startup_size - 0x100); |
| 141 | // -- Dump imagefs.bin |
| 142 | QFileSystem::writeFile("imagefs.bin", _offset + boot_size + startup_size, maxSize - boot_size - startup_size); |
| 143 | |
| 144 | // Display result |
| 145 | QDesktopServices::openUrl(QUrl(_path)); |
| 146 | return true; |
| 147 | } |
| 148 | } |