2016-03-21 18:34:45 +08:00
/* fssbuilder.cpp
2015-04-02 16:04:37 +08:00
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 .
*/
2015-05-15 01:15:19 +08:00
# include "ffsbuilder.h"
2016-10-10 14:05:04 +08:00
# include "descriptor.h"
# include "ffs.h"
# include "peimage.h"
# include "utility.h"
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : erase ( const UModelIndex & index , UByteArray & erased )
2015-05-15 01:15:19 +08:00
{
2015-06-20 02:26:45 +08:00
// Sanity check
if ( ! index . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_INVALID_PARAMETER ;
2015-06-20 02:26:45 +08:00
2016-10-28 00:31:15 +08:00
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : build ( const UModelIndex & root , UByteArray & image )
2016-03-01 15:20:44 +08:00
{
// Sanity check
if ( ! root . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_INVALID_PARAMETER ;
2016-03-01 15:20:44 +08:00
if ( model - > type ( root ) = = Types : : Capsule ) {
return buildCapsule ( root , image ) ;
}
else if ( model - > type ( root ) = = Types : : Image ) {
if ( model - > subtype ( root ) = = Subtypes : : IntelImage ) {
return buildIntelImage ( root , image ) ;
}
2016-03-21 18:34:45 +08:00
else if ( model - > subtype ( root ) = = Subtypes : : UefiImage ) {
2016-03-01 15:20:44 +08:00
return buildRawArea ( root , image ) ;
}
}
2016-07-07 13:57:45 +08:00
return U_NOT_IMPLEMENTED ;
2016-03-01 15:20:44 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildCapsule ( const UModelIndex & index , UByteArray & capsule )
2015-05-15 01:15:19 +08:00
{
// Sanity check
if ( ! index . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
// No action required
if ( model - > action ( index ) = = Actions : : NoAction ) {
2015-12-30 06:39:43 +08:00
// Use original item data
2015-05-15 01:15:19 +08:00
capsule = model - > header ( index ) . append ( model - > body ( index ) ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
// Rebuild or Replace
else if ( model - > action ( index ) = = Actions : : Rebuild
| | model - > action ( index ) = = Actions : : Replace ) {
if ( model - > rowCount ( index ) ) {
2016-07-07 13:57:45 +08:00
// Clear the supplied UByteArray
2015-05-15 01:15:19 +08:00
capsule . clear ( ) ;
2015-12-30 06:39:43 +08:00
// Right now there is only one capsule image element supported
if ( model - > rowCount ( index ) ! = 1 ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildCapsule: building of capsules with %1 elements are not supported, original item data is used").arg(model->rowCount(index)), index);
2015-12-30 06:39:43 +08:00
// Use original item data
capsule = model - > header ( index ) . append ( model - > body ( index ) ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
2015-12-30 06:39:43 +08:00
// Build image
2016-07-07 13:57:45 +08:00
UModelIndex imageIndex = index . child ( 0 , 0 ) ;
UByteArray imageData ;
2015-12-30 06:39:43 +08:00
// Check image type
if ( model - > type ( imageIndex ) = = Types : : Image ) {
2016-07-07 13:57:45 +08:00
USTATUS result = U_SUCCESS ;
2016-03-01 15:20:44 +08:00
if ( model - > subtype ( imageIndex ) = = Subtypes : : IntelImage ) {
2015-12-30 06:39:43 +08:00
result = buildIntelImage ( imageIndex , imageData ) ;
2016-03-01 15:20:44 +08:00
}
else if ( model - > subtype ( imageIndex ) = = Subtypes : : UefiImage ) {
2015-12-30 06:39:43 +08:00
result = buildRawArea ( imageIndex , imageData ) ;
2016-03-01 15:20:44 +08:00
}
else {
2016-07-07 13:57:45 +08:00
//msg(UString("buildCapsule: unexpected item of subtype %1 can't be processed, original item data is used").arg(model->subtype(imageIndex)), imageIndex);
2016-03-01 15:20:44 +08:00
capsule . append ( model - > header ( imageIndex ) ) . append ( model - > body ( imageIndex ) ) ;
}
2015-12-30 06:39:43 +08:00
// Check build result
if ( result ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildCapsule: building of \"%1\" failed with error \"%2\", original item data is used").arg(model->name(imageIndex)).arg(errorCodeToUString(result)), imageIndex);
2015-12-30 06:39:43 +08:00
capsule . append ( model - > header ( imageIndex ) ) . append ( model - > body ( imageIndex ) ) ;
}
else
capsule . append ( imageData ) ;
}
else {
2016-07-07 13:57:45 +08:00
//msg(UString("buildCapsule: unexpected item of type %1 can't be processed, original item data is used").arg(model->type(imageIndex)), imageIndex);
2015-12-30 06:39:43 +08:00
capsule . append ( model - > header ( imageIndex ) ) . append ( model - > body ( imageIndex ) ) ;
}
2015-05-15 01:15:19 +08:00
// Check size of reconstructed capsule, it must remain the same
2015-06-20 02:26:45 +08:00
UINT32 newSize = capsule . size ( ) ;
UINT32 oldSize = model - > body ( index ) . size ( ) ;
if ( newSize > oldSize ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildCapsule: new capsule body size %1h (%2) is bigger than the original %3h (%4)")
// .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize),index);
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
}
2015-06-20 02:26:45 +08:00
else if ( newSize < oldSize ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildCapsule: new capsule body size %1h (%2) is smaller than the original %3h (%4)")
// .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index);
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
}
}
else
capsule = model - > body ( index ) ;
// Build successful, append header
capsule = model - > header ( index ) . append ( capsule ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
2015-06-20 02:26:45 +08:00
2016-07-07 13:57:45 +08:00
//msg(UString("buildCapsule: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index);
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildIntelImage ( const UModelIndex & index , UByteArray & intelImage )
2015-05-15 01:15:19 +08:00
{
2016-01-28 07:21:51 +08:00
// Sanity check
2015-06-20 02:26:45 +08:00
if ( ! index . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-12-30 06:39:43 +08:00
2015-06-20 02:26:45 +08:00
// No action
if ( model - > action ( index ) = = Actions : : NoAction ) {
intelImage = model - > header ( index ) . append ( model - > body ( index ) ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-06-20 02:26:45 +08:00
}
2016-03-01 15:20:44 +08:00
// Rebuild
2015-06-20 02:26:45 +08:00
else if ( model - > action ( index ) = = Actions : : Rebuild ) {
intelImage . clear ( ) ;
2016-03-01 15:20:44 +08:00
2015-12-30 06:39:43 +08:00
// First child will always be descriptor for this type of image, and it's read only
2016-03-01 15:20:44 +08:00
intelImage . append ( model - > header ( index . child ( 0 , 0 ) ) . append ( model - > body ( index . child ( 0 , 0 ) ) ) ) ;
2015-12-30 06:39:43 +08:00
2016-03-01 15:20:44 +08:00
// Process other regions
2015-06-20 02:26:45 +08:00
for ( int i = 1 ; i < model - > rowCount ( index ) ; i + + ) {
2016-07-07 13:57:45 +08:00
UModelIndex currentRegion = index . child ( i , 0 ) ;
2016-03-01 15:20:44 +08:00
2015-12-30 06:39:43 +08:00
// Skip regions with Remove action
if ( model - > action ( currentRegion ) = = Actions : : Remove )
continue ;
// Check item type to be either region or padding
UINT8 type = model - > type ( currentRegion ) ;
if ( type = = Types : : Padding ) {
2016-03-01 15:20:44 +08:00
// Add padding as is
intelImage . append ( model - > header ( currentRegion ) . append ( model - > body ( currentRegion ) ) ) ;
2015-12-30 06:39:43 +08:00
continue ;
}
// Check region subtype
2016-07-07 13:57:45 +08:00
USTATUS result ;
UByteArray region ;
2015-12-30 06:39:43 +08:00
UINT8 regionType = model - > subtype ( currentRegion ) ;
switch ( regionType ) {
2015-06-20 02:26:45 +08:00
case Subtypes : : BiosRegion :
case Subtypes : : PdrRegion :
2016-03-01 15:20:44 +08:00
result = buildRawArea ( currentRegion , region ) ;
2015-12-30 06:39:43 +08:00
if ( result ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildIntelImage: building of %1 region failed with error \"%2\", original item data is used").arg(regionTypeToQString(regionType)).arg(errorCodeToQString(result)), currentRegion);
2016-03-01 15:20:44 +08:00
region = model - > header ( currentRegion ) . append ( model - > body ( currentRegion ) ) ;
2015-12-30 06:39:43 +08:00
}
break ;
2016-03-01 15:20:44 +08:00
case Subtypes : : GbeRegion :
case Subtypes : : MeRegion :
2015-12-30 06:39:43 +08:00
case Subtypes : : EcRegion :
2016-03-01 15:20:44 +08:00
case Subtypes : : Reserved1Region :
case Subtypes : : Reserved2Region :
case Subtypes : : Reserved3Region :
case Subtypes : : Reserved4Region :
// Add region as is
region = model - > header ( currentRegion ) . append ( model - > body ( currentRegion ) ) ;
2015-06-20 02:26:45 +08:00
break ;
default :
2016-07-07 13:57:45 +08:00
msg ( UString ( " buildIntelImage: don't know how to build region of unknown type " ) , index ) ;
return U_UNKNOWN_ITEM_TYPE ;
2015-06-20 02:26:45 +08:00
}
2015-12-30 06:39:43 +08:00
2016-03-01 15:20:44 +08:00
// Append the resulting region
intelImage . append ( region ) ;
2015-06-20 02:26:45 +08:00
}
2016-01-28 07:21:51 +08:00
2015-06-20 02:26:45 +08:00
// Check size of new image, it must be same as old one
UINT32 newSize = intelImage . size ( ) ;
UINT32 oldSize = model - > body ( index ) . size ( ) ;
if ( newSize > oldSize ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildIntelImage: new image size %1h (%2) is bigger than the original %3h (%4)")
// .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index);
return U_INVALID_PARAMETER ;
2015-06-20 02:26:45 +08:00
}
else if ( newSize < oldSize ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildIntelImage: new image size %1h (%2) is smaller than the original %3h (%4)")
// .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index);
return U_INVALID_PARAMETER ;
2015-06-20 02:26:45 +08:00
}
// Reconstruction successful
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-06-20 02:26:45 +08:00
}
2016-07-07 13:57:45 +08:00
//msg(UString("buildIntelImage: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index);
return U_NOT_IMPLEMENTED ;
2015-06-20 02:26:45 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildRawArea ( const UModelIndex & index , UByteArray & rawArea , bool addHeader )
2015-05-15 01:15:19 +08:00
{
// Sanity check
if ( ! index . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
// No action required
if ( model - > action ( index ) = = Actions : : NoAction ) {
rawArea = model - > header ( index ) . append ( model - > body ( index ) ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
// Rebuild or Replace
else if ( model - > action ( index ) = = Actions : : Rebuild
| | model - > action ( index ) = = Actions : : Replace ) {
if ( model - > rowCount ( index ) ) {
2016-07-07 13:57:45 +08:00
// Clear the supplied UByteArray
2015-05-15 01:15:19 +08:00
rawArea . clear ( ) ;
2015-06-20 02:26:45 +08:00
// Build children
2015-05-15 01:15:19 +08:00
for ( int i = 0 ; i < model - > rowCount ( index ) ; i + + ) {
2016-07-07 13:57:45 +08:00
USTATUS result = U_SUCCESS ;
UModelIndex currentChild = index . child ( i , 0 ) ;
UByteArray currentData ;
2015-05-15 01:15:19 +08:00
// Check child type
if ( model - > type ( currentChild ) = = Types : : Volume ) {
result = buildVolume ( currentChild , currentData ) ;
}
else if ( model - > type ( currentChild ) = = Types : : Padding ) {
result = buildPadding ( currentChild , currentData ) ;
}
else {
2016-07-07 13:57:45 +08:00
//msg(UString("buildRawArea: unexpected item of type %1 can't be processed, original item data is used").arg(model->type(currentChild)), currentChild);
2015-05-15 01:15:19 +08:00
currentData = model - > header ( currentChild ) . append ( model - > body ( currentChild ) ) ;
}
// Check build result
if ( result ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildRawArea: building of %1 failed with error \"%2\", original item data is used").arg(model->name(currentChild)).arg(errorCodeToQString(result)), currentChild);
2015-05-15 01:15:19 +08:00
currentData = model - > header ( currentChild ) . append ( model - > body ( currentChild ) ) ;
}
// Append current data
rawArea . append ( currentData ) ;
}
2015-06-20 02:26:45 +08:00
// Check size of new raw area, it must be same as original one
UINT32 newSize = rawArea . size ( ) ;
UINT32 oldSize = model - > body ( index ) . size ( ) ;
if ( newSize > oldSize ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildRawArea: new area size %1h (%2) is bigger than the original %3h (%4)")
// .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index);
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
}
2015-06-20 02:26:45 +08:00
else if ( newSize < oldSize ) {
2016-07-07 13:57:45 +08:00
//msg(UString("buildRawArea: new area size %1h (%2) is smaller than the original %3h (%4)")
// .hexarg(newSize).arg(newSize).hexarg(oldSize).arg(oldSize), index);
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
}
}
else
rawArea = model - > body ( index ) ;
2015-06-20 02:26:45 +08:00
// Build successful, add header if needed
if ( addHeader )
rawArea = model - > header ( index ) . append ( rawArea ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
//msg(UString("buildRawArea: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index);
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildPadding ( const UModelIndex & index , UByteArray & padding )
2015-05-15 01:15:19 +08:00
{
// Sanity check
if ( ! index . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
// No action required
if ( model - > action ( index ) = = Actions : : NoAction ) {
padding = model - > header ( index ) . append ( model - > body ( index ) ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
// Erase
else if ( model - > action ( index ) = = Actions : : Erase ) {
2015-06-20 02:26:45 +08:00
padding = model - > header ( index ) . append ( model - > body ( index ) ) ;
if ( erase ( index , padding ) )
2016-07-07 13:57:45 +08:00
msg ( UString ( " buildPadding: erase failed, original item data is used " ) , index ) ;
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
//msg(UString("buildPadding: unexpected action \"%1\"").arg(actionTypeToUString(model->action(index))), index);
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildNonUefiData ( const UModelIndex & index , UByteArray & data )
2015-05-15 01:15:19 +08:00
{
// Sanity check
if ( ! index . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
// No action required
if ( model - > action ( index ) = = Actions : : NoAction ) {
data = model - > header ( index ) . append ( model - > body ( index ) ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
// Erase
else if ( model - > action ( index ) = = Actions : : Erase ) {
2015-06-20 02:26:45 +08:00
data = model - > header ( index ) . append ( model - > body ( index ) ) ;
if ( erase ( index , data ) )
2016-07-07 13:57:45 +08:00
msg ( UString ( " buildNonUefiData: erase failed, original item data is used " ) , index ) ;
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
//msg(UString("buildNonUefiData: unexpected action \"%1\"").arg(actionTypeToUString(model->action(index))), index);
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildFreeSpace ( const UModelIndex & index , UByteArray & freeSpace )
2015-05-15 01:15:19 +08:00
{
// Sanity check
if ( ! index . isValid ( ) )
2016-07-07 13:57:45 +08:00
return U_INVALID_PARAMETER ;
2015-05-15 01:15:19 +08:00
// No action required
if ( model - > action ( index ) = = Actions : : NoAction ) {
freeSpace = model - > header ( index ) . append ( model - > body ( index ) ) ;
2016-07-07 13:57:45 +08:00
return U_SUCCESS ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
//msg(UString("buildFreeSpace: unexpected action \"%1\"").arg(actionTypeToQString(model->action(index))), index);
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildVolume ( const UModelIndex & index , UByteArray & volume )
2015-06-20 02:26:45 +08:00
{
2016-07-07 13:57:45 +08:00
return U_NOT_IMPLEMENTED ;
2015-06-20 02:26:45 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildPadFile ( const UModelIndex & index , UByteArray & padFile )
2015-05-15 01:15:19 +08:00
{
2016-07-07 13:57:45 +08:00
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildFile ( const UModelIndex & index , UByteArray & file )
2015-05-15 01:15:19 +08:00
{
2016-07-07 13:57:45 +08:00
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-07-07 13:57:45 +08:00
USTATUS FfsBuilder : : buildSection ( const UModelIndex & index , UByteArray & section )
2015-05-15 01:15:19 +08:00
{
2016-07-07 13:57:45 +08:00
return U_NOT_IMPLEMENTED ;
2015-05-15 01:15:19 +08:00
}
2016-03-01 15:20:44 +08:00