2015-04-02 16:07:32 +08:00
|
|
|
/* fssops.cpp
|
|
|
|
|
|
|
|
Copyright (c) 2015, Nikolaj Schlej. All rights reserved.
|
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ffsops.h"
|
2016-10-10 14:05:04 +08:00
|
|
|
#include "ffs.h"
|
|
|
|
#include "utility.h"
|
2018-08-23 13:43:46 +08:00
|
|
|
#include "nvramparser.h"
|
2015-04-02 16:07:32 +08:00
|
|
|
|
2016-07-07 13:57:45 +08:00
|
|
|
USTATUS FfsOperations::extract(const UModelIndex & index, UString & name, UByteArray & extracted, const UINT8 mode)
|
2015-04-02 16:07:32 +08:00
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if (!index.isValid())
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2015-04-02 16:07:32 +08:00
|
|
|
|
2016-04-17 07:25:44 +08:00
|
|
|
// Default name
|
2016-10-28 00:31:15 +08:00
|
|
|
name = uniqueItemName(index);
|
2015-04-02 16:07:32 +08:00
|
|
|
|
|
|
|
// Get extracted data
|
|
|
|
if (mode == EXTRACT_MODE_AS_IS) {
|
|
|
|
// Extract as is, with header body and tail
|
|
|
|
extracted.clear();
|
2018-08-23 16:00:29 +08:00
|
|
|
extracted += model->header(index);
|
|
|
|
extracted += model->body(index);
|
|
|
|
extracted += model->tail(index);
|
2015-04-02 16:07:32 +08:00
|
|
|
}
|
|
|
|
else if (mode == EXTRACT_MODE_BODY) {
|
2016-03-01 15:20:44 +08:00
|
|
|
name += QObject::tr("_body");
|
2015-04-02 16:07:32 +08:00
|
|
|
// Extract without header and tail
|
|
|
|
extracted.clear();
|
2018-08-23 16:00:29 +08:00
|
|
|
extracted += model->body(index);
|
2015-04-02 16:07:32 +08:00
|
|
|
}
|
2015-09-01 03:34:42 +08:00
|
|
|
else if (mode == EXTRACT_MODE_BODY_UNCOMPRESSED) {
|
2016-03-01 15:20:44 +08:00
|
|
|
name += QObject::tr("_body_unc");
|
2015-09-01 03:34:42 +08:00
|
|
|
// Extract without header and tail, uncompressed
|
|
|
|
extracted.clear();
|
|
|
|
// There is no need to redo decompression, we can use child items
|
|
|
|
for (int i = 0; i < model->rowCount(index); i++) {
|
2016-07-07 13:57:45 +08:00
|
|
|
UModelIndex childIndex = index.child(i, 0);
|
2016-04-21 04:41:18 +08:00
|
|
|
// Ensure 4-byte alignment of current section
|
2018-08-23 16:00:29 +08:00
|
|
|
extracted += UByteArray('\x00', ALIGN4((UINT32)extracted.size()) - (UINT32)extracted.size());
|
2016-04-21 04:41:18 +08:00
|
|
|
// Add current section header, body and tail
|
2018-08-23 16:00:29 +08:00
|
|
|
extracted += model->header(childIndex);
|
|
|
|
extracted += model->body(childIndex);
|
|
|
|
extracted += model->tail(childIndex);
|
2015-09-01 03:34:42 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-02 16:07:32 +08:00
|
|
|
else
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_UNKNOWN_EXTRACT_MODE;
|
2015-04-02 16:07:32 +08:00
|
|
|
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_SUCCESS;
|
2015-04-02 16:07:32 +08:00
|
|
|
}
|
|
|
|
|
2018-08-23 13:43:46 +08:00
|
|
|
USTATUS FfsOperations::replace(const UModelIndex & index, UByteArray & data, const UINT8 mode)
|
2016-03-01 15:20:44 +08:00
|
|
|
{
|
2018-08-23 13:43:46 +08:00
|
|
|
if (!index.isValid() || !index.parent().isValid())
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2016-03-01 15:20:44 +08:00
|
|
|
|
2018-08-23 13:43:46 +08:00
|
|
|
USTATUS result;
|
|
|
|
UByteArray empty = "";
|
|
|
|
FfsParser parser(model);
|
|
|
|
UINT32 localOffset = model->offset(index) + model->header(index).size();
|
|
|
|
UModelIndex index_out;
|
|
|
|
|
|
|
|
if(mode == REPLACE_MODE_BODY)
|
|
|
|
data = model->header(index) + data;
|
|
|
|
|
|
|
|
if (model->type(index) == Types::Region) {
|
|
|
|
UINT8 type = model->subtype(index);
|
|
|
|
switch (type) {
|
|
|
|
case Subtypes::BiosRegion:
|
|
|
|
result = parser.parseBiosRegion(data, localOffset, index, index_out, CREATE_MODE_AFTER);
|
|
|
|
break;
|
|
|
|
case Subtypes::MeRegion:
|
|
|
|
result = parser.parseMeRegion(data, localOffset, index, index_out, CREATE_MODE_AFTER);
|
|
|
|
break;
|
|
|
|
case Subtypes::GbeRegion:
|
|
|
|
result = parser.parseGbeRegion(data, localOffset, index, index_out, CREATE_MODE_AFTER);
|
|
|
|
break;
|
|
|
|
case Subtypes::PdrRegion:
|
|
|
|
result = parser.parsePdrRegion(data, localOffset, index, index_out, CREATE_MODE_AFTER);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return U_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result && result != U_VOLUMES_NOT_FOUND && result != U_INVALID_VOLUME)
|
|
|
|
return result;
|
2016-03-01 15:20:44 +08:00
|
|
|
}
|
2018-08-23 13:43:46 +08:00
|
|
|
else if (model->type(index) == Types::Padding) {
|
|
|
|
// Get info
|
|
|
|
QString name = usprintf("Padding");
|
|
|
|
QString info = usprintf("Full size: %Xh (%u)", data.size(), data.size());
|
|
|
|
// Add tree item
|
|
|
|
//!TODO UModelIndex fileIndex = model->addItem(Types::Padding, getPaddingType(body), COMPRESSION_ALGORITHM_NONE, name, "", info, UByteArray(), body, index, mode);
|
2016-03-01 15:20:44 +08:00
|
|
|
}
|
2018-08-23 13:43:46 +08:00
|
|
|
else if (model->type(index) == Types::Volume) {
|
|
|
|
result = parser.parseVolumeHeader(data, localOffset, index, index_out, CREATE_MODE_AFTER);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
result = parser.parseVolumeBody(index_out);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (model->type(index) == Types::File) {
|
|
|
|
result = parser.parseFileHeader(data, localOffset, index, index_out, CREATE_MODE_AFTER);
|
|
|
|
if (result && result != U_VOLUMES_NOT_FOUND && result != U_INVALID_VOLUME)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
result = parser.parseFileBody(index_out);
|
|
|
|
if (result && result != U_VOLUMES_NOT_FOUND && result != U_INVALID_VOLUME)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (model->type(index) == Types::Section) {
|
|
|
|
result = parser.parseSectionHeader(data, localOffset, index, index_out, true, CREATE_MODE_AFTER);
|
|
|
|
if (result && result != U_VOLUMES_NOT_FOUND && result != U_INVALID_VOLUME)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
result = parser.parseSectionBody(index_out);
|
|
|
|
if(result && result != U_VOLUMES_NOT_FOUND && result != U_INVALID_VOLUME)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if(model->type(index) == Types::EvsaStore || model->type(index) == Types::CmdbStore ||
|
|
|
|
model->type(index) == Types::FdcStore || model->type(index) == Types::FlashMapStore ||
|
|
|
|
model->type(index) == Types::FsysStore || model->type(index) == Types::FtwStore ||
|
|
|
|
model->type(index) == Types::Vss2Store || model->type(index) == Types::VssStore) {
|
|
|
|
if(data.size() != model->header(index).size() + model->body(index).size())
|
|
|
|
return U_INVALID_STORE_SIZE;
|
|
|
|
|
|
|
|
NvramParser nvramParser(model, &parser);
|
|
|
|
|
|
|
|
result = nvramParser.parseStoreHeader(data, localOffset, index, index_out, CREATE_MODE_AFTER);
|
|
|
|
if (result && result != U_VOLUMES_NOT_FOUND && result != U_INVALID_VOLUME)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
UINT8 type = model->type(index);
|
|
|
|
switch (type) {
|
|
|
|
case Types::FdcStore: nvramParser.parseFdcStoreBody(index_out); break;
|
|
|
|
case Types::VssStore: nvramParser.parseVssStoreBody(index_out, 0); break;
|
|
|
|
case Types::Vss2Store: nvramParser.parseVssStoreBody(index_out, 4); break;
|
|
|
|
case Types::FsysStore: nvramParser.parseFsysStoreBody(index_out); break;
|
|
|
|
case Types::EvsaStore: nvramParser.parseEvsaStoreBody(index_out); break;
|
|
|
|
case Types::FlashMapStore: nvramParser.parseFlashMapBody(index_out); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return U_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
// Set remove action to replaced item
|
|
|
|
model->setAction(index, Actions::Remove);
|
|
|
|
model->setAction(index_out, Actions::Replace);
|
|
|
|
|
|
|
|
// Rebuild parent, if it has no action now
|
|
|
|
UModelIndex parent = index.parent();
|
|
|
|
if (parent.isValid() && model->type(parent) != Types::Root
|
|
|
|
&& model->action(parent) == Actions::NoAction)
|
|
|
|
rebuild(parent);
|
|
|
|
|
|
|
|
return U_SUCCESS;
|
2016-03-01 15:20:44 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 13:57:45 +08:00
|
|
|
USTATUS FfsOperations::remove(const UModelIndex & index)
|
2016-03-01 15:20:44 +08:00
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if (!index.isValid())
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2016-03-01 15:20:44 +08:00
|
|
|
|
|
|
|
// Set remove action
|
|
|
|
model->setAction(index, Actions::Remove);
|
|
|
|
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_SUCCESS;
|
2016-03-01 15:20:44 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 13:57:45 +08:00
|
|
|
USTATUS FfsOperations::rebuild(const UModelIndex & index)
|
2016-03-01 15:20:44 +08:00
|
|
|
{
|
|
|
|
// Sanity check
|
|
|
|
if (!index.isValid())
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2016-03-01 15:20:44 +08:00
|
|
|
|
|
|
|
// On insert action, set insert action for children
|
|
|
|
//if (action == Actions::Insert)
|
|
|
|
// for (int i = 0; i < item->childCount(); i++)
|
|
|
|
// setAction(index.child(i, 0), Actions::Insert);
|
|
|
|
|
|
|
|
// Set rebuild action
|
|
|
|
model->setAction(index, Actions::Rebuild);
|
|
|
|
|
|
|
|
// Rebuild parent, if it has no action now
|
2016-07-07 13:57:45 +08:00
|
|
|
UModelIndex parent = index.parent();
|
2016-03-01 15:20:44 +08:00
|
|
|
if (parent.isValid() && model->type(parent) != Types::Root
|
|
|
|
&& model->action(parent) == Actions::NoAction)
|
|
|
|
rebuild(parent);
|
|
|
|
|
2016-07-07 13:57:45 +08:00
|
|
|
return U_SUCCESS;
|
2016-03-01 15:20:44 +08:00
|
|
|
}
|