3 * Copyright (c) 2012-2014, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <Library/IoLib.h>
16 #include <Library/NorFlashPlatformLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/MemoryAllocationLib.h>
20 #include <Protocol/SimpleFileSystem.h>
22 #include "BootMonFsInternal.h"
34 ASSERT (Size
% 4 == 0);
43 if (Word
> ~Checksum
) {
54 BootMonFsComputeFooterChecksum (
55 IN OUT HW_IMAGE_DESCRIPTION
*Footer
58 HW_IMAGE_DESCRIPTION
*Description
;
61 Footer
->Attributes
= 1;
63 Description
= AllocateZeroPool (sizeof (HW_IMAGE_DESCRIPTION
));
64 if (Description
== NULL
) {
65 DEBUG ((DEBUG_ERROR
, "BootMonFsComputeFooterChecksum: Unable to allocate memory.\n"));
66 return EFI_OUT_OF_RESOURCES
;
69 // Copy over to temporary shim
70 CopyMem (Description
, Footer
, sizeof (HW_IMAGE_DESCRIPTION
));
72 // BootMon doesn't checksum the previous checksum
73 Description
->FooterChecksum
= 0;
75 // Blank out regions which aren't being used.
76 for (Index
= Footer
->RegionCount
; Index
< HW_IMAGE_DESCRIPTION_REGION_MAX
; Index
++) {
77 Description
->Region
[Index
].Checksum
= 0;
78 Description
->Region
[Index
].LoadAddress
= 0;
79 Description
->Region
[Index
].Offset
= 0;
80 Description
->Region
[Index
].Size
= 0;
83 // Compute the checksum
84 Footer
->FooterChecksum
= BootMonFsChecksum (Description
, sizeof (HW_IMAGE_DESCRIPTION
));
86 FreePool (Description
);
92 BootMonFsIsImageValid (
93 IN HW_IMAGE_DESCRIPTION
*Desc
,
98 HW_IMAGE_FOOTER
*Footer
;
101 Footer
= &Desc
->Footer
;
103 // Check that the verification bytes are present
104 if ((Footer
->FooterSignature1
!= HW_IMAGE_FOOTER_SIGNATURE_1
) ||
105 (Footer
->FooterSignature2
!= HW_IMAGE_FOOTER_SIGNATURE_2
)) {
109 if (Footer
->Version
== HW_IMAGE_FOOTER_VERSION
) {
110 if (Footer
->Offset
!= HW_IMAGE_FOOTER_OFFSET
) {
113 } else if (Footer
->Version
== HW_IMAGE_FOOTER_VERSION2
) {
114 if (Footer
->Offset
!= HW_IMAGE_FOOTER_OFFSET2
) {
121 Checksum
= Desc
->FooterChecksum
;
122 Status
= BootMonFsComputeFooterChecksum (Desc
);
123 if (EFI_ERROR (Status
)) {
124 DEBUG ((DEBUG_ERROR
, "Warning: failed to compute checksum for image '%a'\n", Desc
->Footer
.Filename
));
127 if (Desc
->FooterChecksum
!= Checksum
) {
128 DEBUG ((DEBUG_ERROR
, "Warning: image '%a' checksum mismatch.\n", Desc
->Footer
.Filename
));
131 if ((Desc
->BlockEnd
!= Lba
) || (Desc
->BlockStart
> Desc
->BlockEnd
)) {
139 BootMonFsDiscoverNextImage (
140 IN BOOTMON_FS_INSTANCE
*Instance
,
141 IN EFI_LBA
*LbaStart
,
142 OUT HW_IMAGE_DESCRIPTION
*ImageDescription
145 EFI_DISK_IO_PROTOCOL
*DiskIo
;
150 DiskIo
= Instance
->DiskIo
;
152 CurrentLba
= *LbaStart
;
154 // Look for images in the rest of this block
155 while (CurrentLba
<= Instance
->Media
->LastBlock
) {
156 // Work out the byte offset into media of the image description in this block
157 // If present, the image description is at the very end of the block.
158 DescOffset
= ((CurrentLba
+ 1) * Instance
->Media
->BlockSize
) - sizeof (HW_IMAGE_DESCRIPTION
);
160 // Read the image description from media
161 Status
= DiskIo
->ReadDisk (DiskIo
,
162 Instance
->Media
->MediaId
,
164 sizeof (HW_IMAGE_DESCRIPTION
),
167 if (EFI_ERROR (Status
)) {
171 // If we found a valid image description...
172 if (BootMonFsIsImageValid (ImageDescription
, (CurrentLba
- Instance
->Media
->LowestAlignedLba
))) {
173 DEBUG ((EFI_D_ERROR
, "Found image: %a in block %d.\n", &(ImageDescription
->Footer
.Filename
), (UINTN
)(CurrentLba
- Instance
->Media
->LowestAlignedLba
)));
175 *LbaStart
= ImageDescription
->BlockEnd
+ 1;
182 *LbaStart
= CurrentLba
;
183 return EFI_NOT_FOUND
;
187 BootMonFsInitialize (
188 IN BOOTMON_FS_INSTANCE
*Instance
194 BOOTMON_FS_FILE
*NewFile
;
200 Status
= BootMonFsCreateFile (Instance
, &NewFile
);
201 if (EFI_ERROR (Status
)) {
205 Status
= BootMonFsDiscoverNextImage (Instance
, &Lba
, &(NewFile
->HwDescription
));
206 if (EFI_ERROR (Status
)) {
207 // Free NewFile allocated by BootMonFsCreateFile ()
211 InsertTailList (&Instance
->RootFile
->Link
, &NewFile
->Link
);
215 Instance
->Initialized
= TRUE
;