[edk2] [PATCH 1/1] EmbeddedPkg: Fix multiple Virtual RTC issues

2019-02-25 Thread Pete Batard
LibGetTime():
- Two variables were used for the epoch, where only one should have been.
- Also harmonize variable name to match the one used in LibSetTime.
LiBSetTime():
- Address possible underflows if time is set to start of epoch.
- Ensure that time being read does actually match time that was manually
  set (plus the time elapsed since), by subtracting number of seconds
  since reset.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c | 34 
++--
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git 
a/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c 
b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
index 4c354730d02b..5559106b696f 100644
--- a/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
+++ b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
@@ -54,13 +54,12 @@ LibGetTime (
   )
 {
   EFI_STATUS  Status;
-  UINT32  EpochSeconds;
   INT16   TimeZone;
   UINT8   Daylight;
   UINT64  Freq;
   UINT64  Counter;
   UINT64  Remainder;
-  UINTN   ElapsedSeconds;
+  UINTN   EpochSeconds;
   UINTN   Size;
 
   if (Time == NULL) {
@@ -75,13 +74,13 @@ LibGetTime (
 
   // Get the epoch time from non-volatile storage
   Size = sizeof (UINTN);
-  ElapsedSeconds = 0;
+  EpochSeconds = 0;
   Status = EfiGetVariable (
  (CHAR16 *)mEpochVariableName,
  &gEfiCallerIdGuid,
  NULL,
  &Size,
- (VOID *)&ElapsedSeconds
+ (VOID *)&EpochSeconds
  );
   // Fall back to compilation-time epoch if not set
   if (EFI_ERROR (Status)) {
@@ -93,7 +92,7 @@ LibGetTime (
 // If you are attempting to use this library on such an environment, please
 // contact the edk2 mailing list, so we can try to add support for it.
 //
-ElapsedSeconds = BUILD_EPOCH;
+EpochSeconds = BUILD_EPOCH;
 DEBUG ((
   DEBUG_INFO,
   "LibGetTime: %s non volatile variable was not found - Using compilation 
time epoch.\n",
@@ -101,7 +100,7 @@ LibGetTime (
   ));
   }
   Counter = GetPerformanceCounter ();
-  ElapsedSeconds += DivU64x64Remainder (Counter, Freq, &Remainder);
+  EpochSeconds += DivU64x64Remainder (Counter, Freq, &Remainder);
 
   // Get the current time zone information from non-volatile storage
   Size = sizeof (TimeZone);
@@ -204,7 +203,7 @@ LibGetTime (
 }
   }
 
-  EpochToEfiTime (ElapsedSeconds, Time);
+  EpochToEfiTime (EpochSeconds, Time);
 
   // Because we use the performance counter, we can fill the Nanosecond 
attribute
   // provided that the remainder doesn't overflow 64-bit during multiplication.
@@ -240,6 +239,9 @@ LibSetTime (
   )
 {
   EFI_STATUS  Status;
+  UINT64  Freq;
+  UINT64  Counter;
+  UINT64  Remainder;
   UINTN   EpochSeconds;
 
   if (!IsTimeValid (Time)) {
@@ -249,16 +251,30 @@ LibSetTime (
   EpochSeconds = EfiTimeToEpoch (Time);
 
   // Adjust for the correct time zone, i.e. convert to UTC time zone
-  if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
+  if ((Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)
+  && (EpochSeconds > Time->TimeZone * SEC_PER_MIN)) {
 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
   }
 
   // Adjust for the correct period
-  if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
+  if (((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT)
+  && (EpochSeconds > SEC_PER_HOUR)) {
 // Convert to un-adjusted time, i.e. fall back one hour
 EpochSeconds -= SEC_PER_HOUR;
   }
 
+  // Use the performance counter to substract the number of seconds
+  // since platform reset. Without this, setting time from the shell
+  // and immediately reading it back would result in a forward time
+  // offset, of the duration during which the platform has been up.
+  Freq = GetPerformanceCounterProperties (NULL, NULL);
+  if (Freq != 0) {
+Counter = GetPerformanceCounter ();
+if (EpochSeconds > DivU64x64Remainder (Counter, Freq, &Remainder)) {
+  EpochSeconds -= DivU64x64Remainder (Counter, Freq, &Remainder);
+}
+  }
+
   // Save the current time zone information into non-volatile storage
   Status = EfiSetVariable (
  (CHAR16 *)mTimeZoneVariableName,
-- 
2.17.0.windows.1

___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


[edk2] [PATCH 0/1] EmbeddedPkg: Fix multiple Virtual RTC issues

2019-02-25 Thread Pete Batard
This patch fixes multiple issues in VirtualRealTimeClockLib.c:

1. Use of two separate variables (ElapsedSeconds and EpochSeconds) in
   LibGetTime(), where only a single one should have been used.

2. Possible underflow while sustracting TZ/DST offsets in LibSetTime()
   if the user sets a date that is very close to start of epoch.

3. Non substraction of elpased seconds since reset, which would lead to
   the following behaviour in UEFI shell (assuming for this example that
   exactly 5 minutes have elapsed since platform reset):

 Shell> time 12:00:00
 Shell> time
 12:05:01 (LOCAL)

   In other words, setting and immediately reading back the time would
   result in the returned value being offset by the time since reset.

   This last behaviour has been observed (and confirmed fixed) using an
   RPi3 platform.

Regards,

/Pete

Pete Batard (1):
  EmbeddedPkg: Fix multiple Virtual RTC issues

 .../VirtualRealTimeClockLib.c | 34 ++-
 1 file changed, 25 insertions(+), 9 deletions(-)

-- 
2.17.0.windows.1

___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH 1/1] EmbeddedPkg/Library: Add VirtualRealTimeClockLib

2019-02-15 Thread Pete Batard

On 2019-02-15 14:39, Ard Biesheuvel wrote:

On Fri, 15 Feb 2019 at 11:07, Ard Biesheuvel  wrote:


On Tue, 12 Feb 2019 at 19:14, Leif Lindholm  wrote:


On Mon, Feb 04, 2019 at 12:47:36PM +, Pete Batard wrote:

This is designed to be used on platforms where a a real RTC is not
available and relies on an RtcEpochSeconds variable having been set or,
if that is not the case, falls back to using the epoch embedded at
compilation time.

Note that, in order to keep things simple for the setting of the
compilation time variable, only GCC environments with UNIX-like shells
and where a 'date' command is available are meant to be supported for
now.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 


On the whole, this looks good to me.
One addition we'll need, so that we can build this library standalone
is an entry in EmbeddedPkg.dsc:

diff --git a/EmbeddedPkg/EmbeddedPkg.dsc b/EmbeddedPkg/EmbeddedPkg.dsc
index 4d9e6399d5..dc5040e611 100644
--- a/EmbeddedPkg/EmbeddedPkg.dsc
+++ b/EmbeddedPkg/EmbeddedPkg.dsc
@@ -218,6 +218,7 @@ [Components.common]
EmbeddedPkg/Library/CoherentDmaLib/CoherentDmaLib.inf
EmbeddedPkg/Library/NonCoherentDmaLib/NonCoherentDmaLib.inf

EmbeddedPkg/Library/DxeDtPlatformDtbLoaderLibDefault/DxeDtPlatformDtbLoaderLibDefault.inf
+  EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
EmbeddedPkg/EmbeddedMonotonicCounter/EmbeddedMonotonicCounter.inf
EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClockRuntimeDxe.inf

I don't have any strong opinions on either of Phil's suggestions, but
if you could give some feedback on those and fold the above in, this
could go in.



WIth this addition

Reviewed-by: Ard Biesheuvel 

Pushed as 1b261a705f94..64a17fadcb79


OK, there is a problem with this code:

+EFI_STATUS
+EFIAPI
+LibGetTime (
+  OUT EFI_TIME   *Time,
+  OUT EFI_TIME_CAPABILITIES  *Capabilities
+  )
+{
+  EFI_STATUS  Status;
+  UINT32  EpochSeconds;

EpochSeconds is declared here, and updated depending on time zone and
DST settings. However, the resulting value is never used anywhere.


You're right.

Looks like I forgot to merge all the use of EpochSeconds into 
ElapsedSeconds, from the code I copy/pasted.


I'm very sorry about this, as it's something I should have picked up 
before sending this patch for review.

It is not clear to me what the correct fix is, so Pete, could you
please look into this?


I'll send a fix for this as soon as I have a chance. Thanks for pointing 
the mistake.


Regards,

/Pete
___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH v5 edk2-platforms 00/22] Platform/RaspberryPi: Add Raspberry Pi 3 support

2019-02-15 Thread Pete Batard

Awesome!

Thanks a lot to all the people on this list who helped with the review 
process, provided advice, or tested this series.


Regards,

/Pete

On 2019-02-15 12:05, Ard Biesheuvel wrote:

On Fri, 15 Feb 2019 at 11:27, Ard Biesheuvel  wrote:


On Wed, 13 Feb 2019 at 04:50, Jeremy Linton  wrote:


the
Hi,

On 2/5/19 10:25 AM, Pete Batard wrote:

Changes for v5:

* Raspberry/Pi3 -> RaspberryPi/RPi3
* Remove VirtualRealTimeClockLib as well as BUILD_EPOCH macro (use the upcoming
EmbeddedPkg Virtual RTC from EDK2 instead)
* Use $(PLATFORM_NAME) where possible in .dsc and .fdf
* Update Readme to remove build instructions, describe ACPI limitations, fix
ATF Readme link and split OS installation & test notes into a separate file.
* Add -Wl,--fix-cortex-a53-843419 to LINK_FLAGS

IMPORTANT: Due to the removal of VirtualRealTimeClockLib this series requires
https://lists.01.org/pipermail/edk2-devel/2019-February/036301.html to have
been applied to your edk2 repository.


I would just like to congratulate everyone working on this port. It is
crazy awesome! I just applied these patches, followed the readme
instructions for the SD card. Then grabbed a USB DVD drive, and a USB
SSD, plugged in a keyboard/mouse/monitor/network and booted the fedora
29 1.2 install ISO. The graphical installer ran as expected, system
installed as expected, and other than the nextboot not being reset (had
to update it via the BDS, as is documented) it "just worked".

There are a few rough edges here/there but the idea that we have another
ARM machine that almost behaves like one expects a modern computing
device to behave, is wonderful. The only real gocha continues to be the
rpi's well known shortcomings, including the fact that it took nearly 6
hours to install.


so,

Tested-by: Jeremy Linton 



Thanks Jeremy.

I fully agree, and I am quite happy to have an example to point to the
next time someone wants to contribute some 96boards UEFI port that
looks like u-boot and only runs on the serial port


For the series

Reviewed-by: Ard Biesheuvel 

edk2-non-osi pieces pushed as

863a3237a74c Platform/RaspberryPi/RPi3: Add ATF binaries
9c52bc5c0f14 Platform/RaspberryPi/RPi3: Add Device Tree binaries
635f97e09aaa Platform/RaspberryPi/RPi3: Add logo driver

edk2-platforms pieces pushed as 06454982de98..94d6843f1a90

Thanks again, Pete, and everyone else for the excellent work.

/me goes and updates his blog



___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH 1/1] EmbeddedPkg/Library: Add VirtualRealTimeClockLib

2019-02-14 Thread Pete Batard

Hi Leif,

On 2019-02-12 19:14, Leif Lindholm wrote:

On Mon, Feb 04, 2019 at 12:47:36PM +, Pete Batard wrote:

This is designed to be used on platforms where a a real RTC is not
available and relies on an RtcEpochSeconds variable having been set or,
if that is not the case, falls back to using the epoch embedded at
compilation time.

Note that, in order to keep things simple for the setting of the
compilation time variable, only GCC environments with UNIX-like shells
and where a 'date' command is available are meant to be supported for
now.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 


On the whole, this looks good to me.


Thanks for the review.


One addition we'll need, so that we can build this library standalone
is an entry in EmbeddedPkg.dsc:

diff --git a/EmbeddedPkg/EmbeddedPkg.dsc b/EmbeddedPkg/EmbeddedPkg.dsc
index 4d9e6399d5..dc5040e611 100644
--- a/EmbeddedPkg/EmbeddedPkg.dsc
+++ b/EmbeddedPkg/EmbeddedPkg.dsc
@@ -218,6 +218,7 @@ [Components.common]
EmbeddedPkg/Library/CoherentDmaLib/CoherentDmaLib.inf
EmbeddedPkg/Library/NonCoherentDmaLib/NonCoherentDmaLib.inf

EmbeddedPkg/Library/DxeDtPlatformDtbLoaderLibDefault/DxeDtPlatformDtbLoaderLibDefault.inf
+  EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
EmbeddedPkg/EmbeddedMonotonicCounter/EmbeddedMonotonicCounter.inf
EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClockRuntimeDxe.inf


Well, I actually tried just that but got a build failure when trying to 
compile it (sorry can't remember exactly what was the issue and I don't 
have access to an edk2 test env at the moment) and I saw that some 
libraries in the same location, such as TimeLib which I believe is used 
by some edk2-platforms, are also not referenced at all in the dsc.


So my take was that not all of the libs are required to be included in 
the dsc, especially if they are meant to be referenced directly.


If you feel this is important, I can look into adding the .dsc ref 
again, though provided I can figure out my issue (which may very well 
boil down to me not being too familiar with compiling a standalone 
EmbeddedPkg) it might be a week or two before I can send an updated patch.


Regards,

/Pete


I don't have any strong opinions on either of Phil's suggestions, but
if you could give some feedback on those and fold the above in, this
could go in.

Regards,

Leif


---
  EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c   | 400 

  EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf |  43 
+++
  2 files changed, 443 insertions(+)

diff --git 
a/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c 
b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
new file mode 100644
index ..4c354730d02b
--- /dev/null
+++ b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
@@ -0,0 +1,400 @@
+/** @file
+ *
+ *  Implement virtual EFI RealTimeClock runtime services.
+ *
+ *  Coypright (c) 2019, Pete Batard 
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2011-2014, ARM Ltd. All rights reserved.
+ *  Copyright (c) 2008-2010, Apple Inc. All rights reserved.
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ *  Based on 
ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
+ *
+ **/
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC CONST CHAR16  mEpochVariableName[] = L"RtcEpochSeconds";
+STATIC CONST CHAR16  mTimeZoneVariableName[]  = L"RtcTimeZone";
+STATIC CONST CHAR16  mDaylightVariableName[]  = L"RtcDaylight";
+
+/**
+   Returns the current time and date information, and the time-keeping 
capabilities
+   of the virtual RTC.
+
+   @param  Time  A pointer to storage to receive a snapshot of 
the current time.
+   @param  Capabilities  An optional pointer to a buffer to receive 
the real time clock
+ device's capabilities.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER Time is NULL.
+   @retval EFI_DEVICE_ERROR  The time could not be retrieved due to 
hardware error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibGetTime (
+  OUT EFI_TIME   *Time,
+  OUT EFI_TIME_CAPABILITIES  *Capabilities
+  )
+{
+  EFI_STATUS  Status;
+  UINT32  EpochSeconds;

Re: [edk2] [PATCH 1/1] EmbeddedPkg/Library: Add VirtualRealTimeClockLib

2019-02-14 Thread Pete Batard

Hi Philippe,

Thanks fort reviewing this patch, and apologies for the late reply.

On 2019-02-05 20:57, Philippe Mathieu-Daudé wrote:

Hi Pete,

On 2/4/19 1:47 PM, Pete Batard wrote:

This is designed to be used on platforms where a a real RTC is not
available and relies on an RtcEpochSeconds variable having been set or,
if that is not the case, falls back to using the epoch embedded at
compilation time.

Note that, in order to keep things simple for the setting of the
compilation time variable, only GCC environments with UNIX-like shells
and where a 'date' command is available are meant to be supported for
now.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c   | 400 

  EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf |  43 
+++
  2 files changed, 443 insertions(+)

diff --git 
a/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c 
b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
new file mode 100644
index ..4c354730d02b
--- /dev/null
+++ b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
@@ -0,0 +1,400 @@
+/** @file
+ *
+ *  Implement virtual EFI RealTimeClock runtime services.
+ *
+ *  Coypright (c) 2019, Pete Batard 
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2011-2014, ARM Ltd. All rights reserved.
+ *  Copyright (c) 2008-2010, Apple Inc. All rights reserved.
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ *  Based on 
ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
+ *
+ **/
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC CONST CHAR16  mEpochVariableName[] = L"RtcEpochSeconds";
+STATIC CONST CHAR16  mTimeZoneVariableName[]  = L"RtcTimeZone";
+STATIC CONST CHAR16  mDaylightVariableName[]  = L"RtcDaylight";
+
+/**
+   Returns the current time and date information, and the time-keeping 
capabilities
+   of the virtual RTC.
+
+   @param  Time  A pointer to storage to receive a snapshot of 
the current time.
+   @param  Capabilities  An optional pointer to a buffer to receive 
the real time clock
+ device's capabilities.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER Time is NULL.
+   @retval EFI_DEVICE_ERROR  The time could not be retrieved due to 
hardware error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibGetTime (
+  OUT EFI_TIME   *Time,
+  OUT EFI_TIME_CAPABILITIES  *Capabilities
+  )
+{
+  EFI_STATUS  Status;
+  UINT32  EpochSeconds;
+  INT16   TimeZone;
+  UINT8   Daylight;
+  UINT64  Freq;
+  UINT64  Counter;
+  UINT64  Remainder;
+  UINTN   ElapsedSeconds;
+  UINTN   Size;
+
+  if (Time == NULL) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  // Get the counter frequency
+  Freq = GetPerformanceCounterProperties (NULL, NULL);
+  if (Freq == 0) {
+return EFI_DEVICE_ERROR;
+  }
+
+  // Get the epoch time from non-volatile storage
+  Size = sizeof (UINTN);
+  ElapsedSeconds = 0;
+  Status = EfiGetVariable (
+ (CHAR16 *)mEpochVariableName,
+ &gEfiCallerIdGuid,
+ NULL,
+ &Size,
+ (VOID *)&ElapsedSeconds
+ );
+  // Fall back to compilation-time epoch if not set
+  if (EFI_ERROR (Status)) {
+ASSERT(Status != EFI_INVALID_PARAMETER);
+ASSERT(Status != EFI_BUFFER_TOO_SMALL);
+//
+// The following is intended to produce a compilation error on build
+// environments where BUILD_EPOCH can not be set from inline shell.
+// If you are attempting to use this library on such an environment, please
+// contact the edk2 mailing list, so we can try to add support for it.
+//


What about:

#ifndef BUILD_EPOCH
#define BUILD_EPOCH 1549396000 /* As of this commit */
#endif


Well, the plan is to see what we can actually do for platforms that 
can't define BUILD_EPOCH to the actual compilation time through shell 
invocation.


Rather than assume that we won't be able to do anything for those, and 
therefore default to an epoch that would have a very large offset even 
when the user went the trouble of recompiling the firmware, I'd rather 
we get a better idea of the environments this might apply to, an

Re: [edk2] [PATCH v5 edk2-platforms 18/22] Platform/RaspberryPi/RPi3 *NON-OSI*: Add ATF binaries

2019-02-07 Thread Pete Batard

On 2019.02.07 02:35, Kinney, Michael D wrote:

Hi Pete,

When I saw it as a single patch series, I did assume all the
patches were for the edk2-platforms repo.  And it looked like
non-OSI binaries were going into the edk2-platforms repo.

Patch #0 did not make this clear either that multiple repos
were targeted.

I have not seen a patch series with content for multiple repos.


Well, new ways of doing things have to be introduced sometimes and, to 
make things a lot more convenient for people who submit work (and 
others), I would pressure the edk2 mailing list to accept this kind of 
multiple edk2-platforms + edk2-non-osi patchset submission *for new 
platforms*, where content that is aimed at non-osi is simply tagged 
*NON-OSI* rather than split into 2 separate patchsets. As a global 
timesaver (since it also makes review of a newly introduced platform 
easier IMO, by not risking people to miss the separately submitted 
non-osi content), it does make a lot of sense to me to do it this way, 
rather than enforce a rigid approach.


It also makes life simpler for people who simply want to test (rather 
than review), as they can just apply the full patchset onto their local 
edk2-platforms repo in one breadth, rather than have to hunt and apply 
from 2 different sets. To me, the advantages of doing things in this 
fashion largely outweigh any potential drawback.



It would be clearer if there were 2 different series.  One for
edk2-platforms repo and one for the edk2-non-osi repo.


I'm not planning to resubmit a new patchset just to split between osi 
and non-osi at this stage.


If I hear a lot of backlash, and there is need for a v6, I may split it, 
but I really think this approach of submitting brand new content to 
non-osi, by simply tagging it *NON-OSI* on the subject line, should be 
deemed as an acceptable way to introduce the non-osi content for a new 
platforms.


Regards,

/Pete



Thanks,

Mike


-Original Message-
From: Pete Batard [mailto:p...@akeo.ie]
Sent: Wednesday, February 6, 2019 4:53 PM
To: Kinney, Michael D ;
edk2-devel@lists.01.org; leif.lindh...@linaro.org
Subject: Re: [edk2] [PATCH v5 edk2-platforms 18/22]
Platform/RaspberryPi/RPi3 *NON-OSI*: Add ATF binaries

Hi Michael,

On 2019.02.06 22:39, Kinney, Michael D wrote:

Hi Pete,

We have the edk2-non-osi repository for binaries.


Not exactly sure what your point is since this patch is
tagged *NON-OSI*
in its subject line which means it is intended to go
into edk2-non-osi,
and we already went over what, in this patch series, we
thought belonged
or did not belong to non-osi. These ATF binaries were
determined to
belong to non-osi (with the intent to remove them
altogether,
eventually, once we have the next dot release of ATF).

Or is the issue that I am submitting non-osi patches as
part of series
that is prefixed [edk2-platform]?


Do some of
the patches in this series really belong there?


The current consensus from previous patchsets
submission is that the 3
patches that are tagged as *NON-OSI* in this series are
meant to be
applied to edk2-non-osi whereas the other 19 are meant
to be applied to
edk2-platforms.

I have to say, if the issue is that you'd like to see
an [edk2-non-osi]
prefix for these patches, rather than a *NON-OSI*
suffix appended, then
doing so is a bit of a pain when you are re-submitting
a large patchset
series. So I do hope that simply submitting everything
as edk2-platforms
and tagging the non-osi ones in the subject line is
okay.

Regards,

/Pete


https://github.com/tianocore/edk2-non-osi

Thanks,

Mike


-Original Message-
From: edk2-devel [mailto:edk2-devel-
boun...@lists.01.org] On Behalf Of Pete Batard
Sent: Tuesday, February 5, 2019 8:26 AM
To: edk2-devel@lists.01.org
Subject: [edk2] [PATCH v5 edk2-platforms 18/22]
Platform/RaspberryPi/RPi3 *NON-OSI*: Add ATF

binaries


These ATF binaries were built from the ATF source
(commit c3859557)
with the custom RPi3 platform options detailed in

the

readme, and with
no modification to the official source whatsoever.

Contributed-under: TianoCore Contribution Agreement

1.1

Signed-off-by: Pete Batard 
---


Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt

|  26 


Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md

|  42 
   Platform/RaspberryPi/RPi3/TrustedFirmware/bl1.bin
| Bin 0 -> 18801 bytes
   Platform/RaspberryPi/RPi3/TrustedFirmware/fip.bin
| Bin 0 -> 41714 bytes
   4 files changed, 68 insertions(+)

diff --git


a/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt



b/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt

new file mode 100644
index ..b98dc643227e
--- /dev/null
+++


b/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt

@@ -0,0 +1,26 @@
+Copyright (c) 2013-2018, ARM Limited and

Contributors.

All rights reserved.
+
+Redistribution and use in source and binary forms,
with or without modification,
+are permitted provided that the following

con

Re: [edk2] [PATCH v5 edk2-platforms 18/22] Platform/RaspberryPi/RPi3 *NON-OSI*: Add ATF binaries

2019-02-06 Thread Pete Batard

Hi Michael,

On 2019.02.06 22:39, Kinney, Michael D wrote:

Hi Pete,

We have the edk2-non-osi repository for binaries.


Not exactly sure what your point is since this patch is tagged *NON-OSI* 
in its subject line which means it is intended to go into edk2-non-osi, 
and we already went over what, in this patch series, we thought belonged 
or did not belong to non-osi. These ATF binaries were determined to 
belong to non-osi (with the intent to remove them altogether, 
eventually, once we have the next dot release of ATF).


Or is the issue that I am submitting non-osi patches as part of series 
that is prefixed [edk2-platform]?



Do some of
the patches in this series really belong there?


The current consensus from previous patchsets submission is that the 3 
patches that are tagged as *NON-OSI* in this series are meant to be 
applied to edk2-non-osi whereas the other 19 are meant to be applied to 
edk2-platforms.


I have to say, if the issue is that you'd like to see an [edk2-non-osi] 
prefix for these patches, rather than a *NON-OSI* suffix appended, then 
doing so is a bit of a pain when you are re-submitting a large patchset 
series. So I do hope that simply submitting everything as edk2-platforms 
and tagging the non-osi ones in the subject line is okay.


Regards,

/Pete


https://github.com/tianocore/edk2-non-osi

Thanks,

Mike


-Original Message-
From: edk2-devel [mailto:edk2-devel-
boun...@lists.01.org] On Behalf Of Pete Batard
Sent: Tuesday, February 5, 2019 8:26 AM
To: edk2-devel@lists.01.org
Subject: [edk2] [PATCH v5 edk2-platforms 18/22]
Platform/RaspberryPi/RPi3 *NON-OSI*: Add ATF binaries

These ATF binaries were built from the ATF source
(commit c3859557)
with the custom RPi3 platform options detailed in the
readme, and with
no modification to the official source whatsoever.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt
|  26 
  Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md
|  42 
  Platform/RaspberryPi/RPi3/TrustedFirmware/bl1.bin
| Bin 0 -> 18801 bytes
  Platform/RaspberryPi/RPi3/TrustedFirmware/fip.bin
| Bin 0 -> 41714 bytes
  4 files changed, 68 insertions(+)

diff --git
a/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt
b/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt
new file mode 100644
index ..b98dc643227e
--- /dev/null
+++
b/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt
@@ -0,0 +1,26 @@
+Copyright (c) 2013-2018, ARM Limited and Contributors.
All rights reserved.
+
+Redistribution and use in source and binary forms,
with or without modification,
+are permitted provided that the following conditions
are met:
+
+* Redistributions of source code must retain the above
copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the
above copyright notice, this
+  list of conditions and the following disclaimer in
the documentation and/or
+  other materials provided with the distribution.
+
+* Neither the name of ARM nor the names of its
contributors may be used to
+  endorse or promote products derived from this
software without specific prior
+  written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
diff --git
a/Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md
b/Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md
new file mode 100644
index ..74bcec7d1f12
--- /dev/null
+++
b/Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md
@@ -0,0 +1,42 @@
+ARM Trusted Firmware for Raspberry Pi 3
+===
+
+The `bl1` and `fip` ATF binaries, found in this
directory, were built from
+the [official ATF source](https://github.com/ARM-
software/arm-trusted-firmware)
+(commit c3859557) using Linaro's GCC 5.5 compiler
with:
+
+```
+export CROSS_COMPILE=/usr/src/gcc-linaro-5.5.0-
2017.10-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
+make PLAT=rpi3 PRELOADED_BL33_BASE=0x3
RPI3_PRELOADED_DTB_BASE=0x1 SUPPORT_VFP=1
RPI3_USE_UEFI_MAP=1 fip all
+```
+
+This results in the following memory mapping:
+
+```
+0x +-+
+

[edk2] [PATCH v5 edk2-platforms 22/22] Platform/RaspberryPi/RPi3: Add platform readme's

2019-02-05 Thread Pete Batard
Documentation is split between general plaform data and OS testing
and installation details.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Readme.md  | 167 
 Platform/RaspberryPi/RPi3/Systems.md |  65 
 Readme.md|   3 +
 3 files changed, 235 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Readme.md 
b/Platform/RaspberryPi/RPi3/Readme.md
new file mode 100644
index ..7434233df0fb
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Readme.md
@@ -0,0 +1,167 @@
+Raspberry Pi Platform
+=
+
+# Summary
+
+This is a port of 64-bit Tiano Core UEFI firmware for the Raspberry Pi 3/3B+ 
platforms,
+based on [Ard Bisheuvel's 
64-bit](http://www.workofard.com/2017/02/uefi-on-the-pi/)
+and [Microsoft's 
32-bit](https://github.com/ms-iot/RPi-UEFI/tree/ms-iot/Pi3BoardPkg)
+implementations, as maintained by [Andrei 
Warkentin](https://github.com/andreiw/RaspberryPiPkg).
+
+This is meant as a generally useful 64-bit ATF + UEFI implementation for the 
Raspberry
+Pi 3/3B+ which should be good enough for most kind of UEFI development, as 
well as for
+running consummer Operating Systems in such as Linux or Windows.
+
+Raspberry Pi is a trademark of the [Raspberry Pi 
Foundation](http://www.raspberrypi.org).
+
+# Status
+
+This firmware, that has been validated to compile against the current
+[edk2](https://github.com/tianocore/edk2)/[edk2-platforms](https://github.com/tianocore/edk2-platforms),
+should be able to boot Linux (SUSE, Ubuntu), NetBSD, FreeBSD as well as 
Windows 10 ARM64
+(full GUI version).
+
+It also provides support for ATF ([Arm Trusted 
Platform](https://github.com/ARM-software/arm-trusted-firmware)).
+
+HDMI and the mini-UART serial port can be used for output devices, with 
mirrored output.
+USB keyboards and the mini-UART serial port can be used as input.
+
+On a freshly built firmware, the default is to boot the UEFI shell.
+To change the default boot order (for instance to boot uSD media by default) 
you
+will need to edit the preferences in _Boot Maintenance Manager_.
+
+For additional information about the tested systems and how to set them up,
+please see [Systems.md](./Systems.md).
+
+# Building
+
+Build instructions from the top level edk2-platforms Readme.md apply.
+
+# Booting the firmware
+
+1. Format a uSD card as FAT32
+2. Copy the generated `RPI_EFI.fd` firmware onto the partition
+3. Download and copy the following files from 
https://github.com/raspberrypi/firmware/tree/master/boot
+  - `bootcode.bin`
+  - `fixup.dat`
+  - `start.elf`
+4. Create a `config.txt` with the following content:
+  ```
+  arm_control=0x200
+  enable_uart=1
+  armstub=RPI_EFI.fd
+  disable_commandline_tags=1
+  ```
+5. Insert the uSD card and power up the Pi.
+
+Note that if you have a model 3+ or a model 3 where you enabled USB boot 
through OTP
+(see 
[here](https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/msd.md))
+you may also be able to boot from a FAT32 USB driver rather than uSD.
+
+# Notes
+
+## ARM Trusted Firmware (ATF)
+
+The ATF binaries being used were compiled from the latest ATF source.
+No aleration to the official source have been applied.
+
+For more details on the ATF compilation, see the 
[Readme](./TrustedFirmware/Readme.md)
+in the `TrustedFirmware/` directory.
+
+## Custom Device Tree
+
+The default Device Tree included in the firmware is the one for a Raspberry Pi 
3 Model B (not B+).
+If you want to use a different Device Tree, to boot a Pi 3 Model B+ for 
instance (for which a
+DTB is also provided under `DeviceTree/`), you should copy the relevant `.dtb` 
into the root of
+the SD or USB, and then edit your `config.txt` so that it looks like:
+
+```
+(...)
+disable_commandline_tags=2
+device_tree_address=0x1
+device_tree_end=0x2
+device_tree=bcm2710-rpi-3-b-plus.dtb
+```
+
+Note: the address range **must** be `[0x1:0x2]`.
+`dtoverlay` and `dtparam` parameters are also supported **when** providing a 
Device Tree`.
+
+## Custom `bootargs`
+
+This firmware will honor the command line passed by the GPU via `cmdline.txt`.
+
+Note, that the ultimate contents of `/chosen/bootargs` are a combination of 
several pieces:
+- Original `/chosen/bootargs` if using the internal DTB. Seems to be 
completely discarded by GPU when booting with a custom device tree.
+- GPU-passed hardware configuration. This one is always present.
+- Additional boot options passed via `cmdline.txt`.
+
+# Limitations
+
+## HDMI
+
+The UEFI HDMI video support relies on the VC (that's the GPU)
+firmware to correctly detect and configure the attached screen.
+Some screens are slow, and this detection may not occur fast
+enough. Finally, you may wish to be able to boot your Pi
+headless, yet be able to attach a display to it later for
+debugging.
+
+To accommodate these issues, the following extra lines
+are recom

[edk2] [PATCH v5 edk2-platforms 21/22] Platform/RaspberryPi/RPi3: Add platform

2019-02-05 Thread Pete Batard
Adds the .dec/.dsc/.fdf needed to build the platform firmware.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/RPi3.dec |  58 ++
 Platform/RaspberryPi/RPi3/RPi3.dsc | 629 
 Platform/RaspberryPi/RPi3/RPi3.fdf | 449 ++
 3 files changed, 1136 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/RPi3.dec 
b/Platform/RaspberryPi/RPi3/RPi3.dec
new file mode 100644
index ..2ca7e746a647
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/RPi3.dec
@@ -0,0 +1,58 @@
+## @file
+#
+#  Copyright (c) 2016, Linaro, Ltd. All rights reserved.
+#  Copyright (c) 2017-2018, Andrei Warkentin 
+#
+#  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.
+#
+##
+
+[Defines]
+  DEC_SPECIFICATION  = 0x0001001A
+  PACKAGE_NAME   = RPi3Pkg
+  PACKAGE_GUID   = DFA0CA8B-F3AC-4607-96AC-46FA04B84DCC
+  PACKAGE_VERSION= 1.0
+
+[Includes]
+  Include
+
+[Protocols]
+  gRaspberryPiFirmwareProtocolGuid = { 0x0ACA9535, 0x7AD0, 0x4286, { 0xB0, 
0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiConfigAppliedProtocolGuid = { 0x0ACA, 0x7AD0, 0x4286, { 
0xB0, 0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiMmcHostProtocolGuid = { 0x3e591c00, 0x9e4a, 0x11df, {0x92, 0x44, 
0x00, 0x02, 0xA5, 0xF5, 0xF5, 0x1B } }
+  gExtendedTextOutputProtocolGuid = { 0x387477ff, 0xffc7, 0xffd2, {0x8e, 0x39, 
0x0, 0xff, 0xc9, 0x69, 0x72, 0x3b } }
+
+[Guids]
+  gRaspberryPiTokenSpaceGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+  gRaspberryPiFdtFileGuid = {0xDF5DA223, 0x1D27, 0x47C3, { 0x8D, 0x1B, 0x9A, 
0x41, 0xB5, 0x5A, 0x18, 0xBC}}
+  gRaspberryPiEventResetGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB4, 0xB4, 0xE4, 0xD4, 0xB4}}
+  gConfigDxeFormSetGuid = {0xCD7CC258, 0x31DB, 0x22E6, {0x9F, 0x22, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+
+[PcdsFixedAtBuild.common]
+  gRaspberryPiTokenSpaceGuid.PcdFdtBaseAddress|0x1|UINT32|0x0001
+  gRaspberryPiTokenSpaceGuid.PcdFirmwareBlockSize|0x0|UINT32|0x0002
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogBase|0x0|UINT32|0x0003
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogSize|0x0|UINT32|0x0004
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageVariableBase|0x0|UINT32|0x0005
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwSpareBase|0x0|UINT32|0x0006
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwWorkingBase|0x0|UINT32|0x0007
+
+[PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx]
+  gRaspberryPiTokenSpaceGuid.PcdCpuClock|0|UINT32|0x000d
+  gRaspberryPiTokenSpaceGuid.PcdSdIsArasan|0|UINT32|0x000e
+  gRaspberryPiTokenSpaceGuid.PcdMmcForce1Bit|0|UINT32|0x000f
+  gRaspberryPiTokenSpaceGuid.PcdMmcForceDefaultSpeed|0|UINT32|0x0010
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdDefaultSpeedMHz|0|UINT32|0x0011
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdHighSpeedMHz|0|UINT32|0x0012
+  gRaspberryPiTokenSpaceGuid.PcdMmcDisableMulti|0|UINT32|0x0013
+  gRaspberryPiTokenSpaceGuid.PcdDebugEnableJTAG|0|UINT32|0x0014
+  gRaspberryPiTokenSpaceGuid.PcdDebugShowUEFIExit|0|UINT32|0x0015
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableVModes|0|UINT32|0x0017
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableSShot|0|UINT32|0x0018
diff --git a/Platform/RaspberryPi/RPi3/RPi3.dsc 
b/Platform/RaspberryPi/RPi3/RPi3.dsc
new file mode 100644
index ..86f9d2e5e925
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/RPi3.dsc
@@ -0,0 +1,629 @@
+# @file
+#
+#  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
+#  Copyright (c) 2014, Linaro Limited. All rights reserved.
+#  Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
+#  Copyright (c) 2017 - 2018, Andrei Warkentin 
+#
+#  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.
+#
+##
+
+
+#
+# Defines Section - statements that will be processed to create a Makefile.
+#
+
+[Defines]
+  PLATFORM_NAME  = RPi3
+  PLATFORM_GUID  

[edk2] [PATCH v5 edk2-platforms 19/22] Platform/RaspberryPi/RPi3 *NON-OSI*: Add Device Tree binaries

2019-02-05 Thread Pete Batard
>From https://github.com/raspberrypi/firmware/tree/master/boot
The .dtb's were decompiled to .dts, and then edited to fix USB
keyboard support as well as CPU enabling through PSCI.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/DeviceTree/License.txt  |  340 ++
 Platform/RaspberryPi/RPi3/DeviceTree/bcm2710-rpi-3-b-plus.dtb |  Bin 0 -> 
25617 bytes
 Platform/RaspberryPi/RPi3/DeviceTree/bcm2710-rpi-3-b-plus.dts | 1263 

 Platform/RaspberryPi/RPi3/DeviceTree/bcm2710-rpi-3-b.dtb  |  Bin 0 -> 
25354 bytes
 Platform/RaspberryPi/RPi3/DeviceTree/bcm2710-rpi-3-b.dts  | 1259 
+++
 5 files changed, 2862 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/DeviceTree/License.txt 
b/Platform/RaspberryPi/RPi3/DeviceTree/License.txt
new file mode 100644
index ..1603937dad82
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/DeviceTree/License.txt
@@ -0,0 +1,340 @@
+GNU GENERAL PUBLIC LICENSE
+   Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Progra

[edk2] [PATCH v5 edk2-platforms 18/22] Platform/RaspberryPi/RPi3 *NON-OSI*: Add ATF binaries

2019-02-05 Thread Pete Batard
These ATF binaries were built from the ATF source (commit c3859557)
with the custom RPi3 platform options detailed in the readme, and with
no modification to the official source whatsoever.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt |  26 
 Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md   |  42 

 Platform/RaspberryPi/RPi3/TrustedFirmware/bl1.bin | Bin 0 -> 18801 bytes
 Platform/RaspberryPi/RPi3/TrustedFirmware/fip.bin | Bin 0 -> 41714 bytes
 4 files changed, 68 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt 
b/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt
new file mode 100644
index ..b98dc643227e
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/TrustedFirmware/License.txt
@@ -0,0 +1,26 @@
+Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without 
modification,
+are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice, 
this
+  list of conditions and the following disclaimer in the documentation and/or
+  other materials provided with the distribution.
+
+* Neither the name of ARM nor the names of its contributors may be used to
+  endorse or promote products derived from this software without specific prior
+  written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md 
b/Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md
new file mode 100644
index ..74bcec7d1f12
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/TrustedFirmware/Readme.md
@@ -0,0 +1,42 @@
+ARM Trusted Firmware for Raspberry Pi 3
+===
+
+The `bl1` and `fip` ATF binaries, found in this directory, were built from
+the [official ATF source](https://github.com/ARM-software/arm-trusted-firmware)
+(commit c3859557) using Linaro's GCC 5.5 compiler with:
+
+```
+export 
CROSS_COMPILE=/usr/src/gcc-linaro-5.5.0-2017.10-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
+make PLAT=rpi3 PRELOADED_BL33_BASE=0x3 RPI3_PRELOADED_DTB_BASE=0x1 
SUPPORT_VFP=1 RPI3_USE_UEFI_MAP=1 fip all
+```
+
+This results in the following memory mapping:
+
+```
+0x +-+
+   |   ROM   | BL1
+0x0001 +-+
+   |   DTB   | (Loaded by the VideoCore)
+0x0002 +-+
+   |   FIP   |
+0x0003 +-+
+   | |
+   |  UEFI PAYLOAD   |
+   | |
+0x0020 +-+
+   |   Secure SRAM   | BL2, BL31
+0x0030 +-+
+   |   Secure DRAM   | BL32 (Secure payload)
+0x0040 +-+
+   | |
+   | |
+   | Non-secure DRAM | BL33
+   | |
+   | |
+0x0100 +-+
+   | |
+   |   ...   |
+   | |
+0x3F00 +-+
+   |   I/O   |
+```
diff --git a/Platform/RaspberryPi/RPi3/TrustedFirmware/bl1.bin 
b/Platform/RaspberryPi/RPi3/TrustedFirmware/bl1.bin
new file mode 100644
index 
..e25138828d0a4ddb24772abb1a60eefc334666b5
GIT binary patch
literal 18801
zcmeHud2|%#mG7;pmedFVtpy|tJ-S*z4DF582<)+{TLO%YF^IMyw%@2*ivhC;Enpe2
zRTADp&Ya2NoOkF$@l}1x-M+hhw@Q@U820D96C>N0$f-gpvNVO-6r%k^_LFjhV53f>
zzYYB^p(#wZhayBm(40?m)v%ZRF-jkU{?@RJdpUDVq_0@Qvl>>=KM8BSMAN6D9c2X^
zv}!gTC9-IZXdThl_aE5oz6A)Ol0}J=O^M4yanPmrUEBE6z%_}qi$f`XQKFzu
zlVZ_?G$rnX41{DgYzORyWMj?6p-TMP#*g+gylWdj3`8X%sIOlv1lmB)R$NV@Mf2q+
zY4d>}JWrcSYDNxGR%4SA-?^DI?>kETG0>;<^-cF@D)C*r!0X4PiJ}^Q9sQugsR%T^
zW2zF8L8

[edk2] [PATCH v5 edk2-platforms 17/22] Platform/RaspberryPi/RPi3: Add USB host driver

2019-02-05 Thread Pete Batard
The Raspberry Pi 3 uses a dedicated Synopsys DesignWare USB Host driver
to interface with various USB peripherals, including the onboard NIC.
This driver, which is based on the Android OpenPlatformPkg implementation,
provides support for that.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/ComponentName.c  |  225 +++
 Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/DriverBinding.c  |  274 
 Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.c   | 1635 

 Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.h   |  162 ++
 Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.inf |   59 +
 Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/DwcHw.h  |  788 
++
 Platform/RaspberryPi/RPi3/Include/Protocol/DwUsb.h  |   53 +
 7 files changed, 3196 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/ComponentName.c 
b/Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/ComponentName.c
new file mode 100644
index ..056debb7c5f4
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/DwUsbHostDxe/ComponentName.c
@@ -0,0 +1,225 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrey Warkentin 
+ *
+ *  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 "DwUsbHostDxe.h"
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  );
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL *This,
+  IN  EFI_HANDLE  ControllerHandle,
+  IN  EFI_HANDLE  ChildHandle,
+  IN  CHAR8   *Language,
+  OUT CHAR16  **ControllerName
+  );
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
+  ComponentNameGetDriverName,
+  ComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)ComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)ComponentNameGetControllerName,
+  "en"
+};
+
+
+STATIC EFI_UNICODE_STRING_TABLE mDriverName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi USB Host Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mDeviceName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi USB Host"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+

[edk2] [PATCH v5 edk2-platforms 16/22] Platform/RaspberryPi/RPi3: Add platform boot manager and helper libs

2019-02-05 Thread Pete Batard
The libraries decide and set the boot order between UEFI Shell, SD media
and/or USB media and allow for user configuration and override. Stale
boot options (non present media) are automatically removed on boot.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Library/PlatformBootManagerLib/PlatformBm.c  
 | 793 
 Platform/RaspberryPi/RPi3/Library/PlatformBootManagerLib/PlatformBm.h  
 |  60 ++
 
Platform/RaspberryPi/RPi3/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
 |  90 +++
 Platform/RaspberryPi/RPi3/Library/PlatformUiAppLib/PlatformUiAppLib.c  
 | 120 +++
 Platform/RaspberryPi/RPi3/Library/PlatformUiAppLib/PlatformUiAppLib.inf
 |  34 +
 5 files changed, 1097 insertions(+)

diff --git 
a/Platform/RaspberryPi/RPi3/Library/PlatformBootManagerLib/PlatformBm.c 
b/Platform/RaspberryPi/RPi3/Library/PlatformBootManagerLib/PlatformBm.c
new file mode 100644
index ..9bbe0db64950
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Library/PlatformBootManagerLib/PlatformBm.c
@@ -0,0 +1,793 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Pete Batard 
+ *  Copyright (c) 2017-2018, Andrei Warkentin 
+ *  Copyright (c) 2016, Linaro Ltd. All rights reserved.
+ *  Copyright (c) 2015-2016, Red Hat, Inc.
+ *  Copyright (c) 2014, ARM Ltd. All rights reserved.
+ *  Copyright (c) 2004-2016, Intel Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "PlatformBm.h"
+
+#define BOOT_PROMPT L"ESC (setup), F1 (shell), ENTER (boot)"
+
+#define DP_NODE_LEN(Type) { (UINT8)sizeof (Type), (UINT8)(sizeof (Type) >> 8) }
+
+#pragma pack (1)
+typedef struct {
+  VENDOR_DEVICE_PATH SerialDxe;
+  UART_DEVICE_PATH   Uart;
+  VENDOR_DEFINED_DEVICE_PATH TermType;
+  EFI_DEVICE_PATH_PROTOCOL   End;
+} PLATFORM_SERIAL_CONSOLE;
+#pragma pack ()
+
+typedef struct {
+  VENDOR_DEVICE_PATH Custom;
+  USB_DEVICE_PATHHub;
+  USB_DEVICE_PATHDev;
+  EFI_DEVICE_PATH_PROTOCOL   EndDevicePath;
+} PLATFORM_USB_DEV;
+
+typedef struct {
+  VENDOR_DEVICE_PATH Custom;
+  EFI_DEVICE_PATH_PROTOCOL   EndDevicePath;
+} PLATFORM_SD_DEV;
+
+#define ARASAN_MMC_DXE_FILE_GUID  \
+  { 0x100c2cfa, 0xb586, 0x4198, { 0x9b, 0x4c, 0x16, 0x83, 0xd1, 0x95, 0xb1, 
0xda } }
+
+#define SDHOST_MMC_DXE_FILE_GUID  \
+  { 0x58abd787, 0xf64d, 0x4ca2, { 0xa0, 0x34, 0xb9, 0xac, 0x2d, 0x5a, 0xd0, 
0xcf } }
+
+#define SERIAL_DXE_FILE_GUID  \
+  { 0xD3987D4B, 0x971A, 0x435F, { 0x8C, 0xAF, 0x49, 0x67, 0xEB, 0x62, 0x72, 
0x41 } }
+
+STATIC PLATFORM_SD_DEV mArasan = {
+  //
+  // VENDOR_DEVICE_PATH ArasanMMCHostDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+ARASAN_MMC_DXE_FILE_GUID
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+STATIC PLATFORM_SD_DEV mSDHost = {
+  //
+  // VENDOR_DEVICE_PATH SdHostDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+SDHOST_MMC_DXE_FILE_GUID
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+STATIC PLATFORM_SERIAL_CONSOLE mSerialConsole = {
+  //
+  // VENDOR_DEVICE_PATH SerialDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+SERIAL_DXE_FILE_GUID
+  },
+
+  //
+  // UART_DEVICE_PATH Uart
+  //
+  {
+{ MESSAGING_DEVICE_PATH, MSG_UART_DP, DP_NODE_LEN (UART_DEVICE_PATH) },
+0,  // Reserved
+FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
+FixedPcdGet8 (PcdUartDefaultDataBits),  // DataBits
+FixedPcdGet8 (PcdUartDefaultParity),// Parity
+FixedPcdGet8 (PcdUartDefaultStopBits)   // StopBits
+  },
+
+  //
+  // VENDOR_DEFINED_DEVICE_PATH TermType
+  //
+  {
+{
+  MESSAGING_DEVICE_PATH, MSG_VENDOR_DP,
+  DP_NODE_LEN (VENDOR_DEFINED_DEVICE_PATH)
+}
+//
+// Guid to be filled in dynamically
+//
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_

[edk2] [PATCH v5 edk2-platforms 15/22] Platform/RaspberryPi/RPi3: Add SD Host driver

2019-02-05 Thread Pete Batard
The BCM283x family includes two SD card controllers, the second one
being the "internal" SD HOST controller. This driver implements support
for this one.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/SdHostDxe/SdHostDxe.c   | 787 

 Platform/RaspberryPi/RPi3/Drivers/SdHostDxe/SdHostDxe.inf |  55 ++
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836SdHost.h |  92 +++
 3 files changed, 934 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/SdHostDxe/SdHostDxe.c 
b/Platform/RaspberryPi/RPi3/Drivers/SdHostDxe/SdHostDxe.c
new file mode 100644
index ..3bf789f96b27
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/SdHostDxe/SdHostDxe.c
@@ -0,0 +1,787 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#define SDHOST_BLOCK_BYTE_LENGTH512
+
+// Driver Timing Parameters
+#define CMD_STALL_AFTER_POLL_US 1
+#define CMD_MIN_POLL_TOTAL_TIME_US  10 // 100ms
+#define CMD_MAX_POLL_COUNT  (CMD_MIN_POLL_TOTAL_TIME_US / 
CMD_STALL_AFTER_POLL_US)
+#define CMD_MAX_RETRY_COUNT 3
+#define CMD_STALL_AFTER_RETRY_US20 // 20us
+#define FIFO_MAX_POLL_COUNT 100
+#define STALL_TO_STABILIZE_US   1 // 10ms
+
+#define IDENT_MODE_SD_CLOCK_FREQ_HZ 40 // 400KHz
+
+// Macros adopted from MmcDxe internal header
+#define SDHOST_R0_READY_FOR_DATABIT8
+#define SDHOST_R0_CURRENTSTATE(Response)((Response >> 9) & 0xF)
+
+#define DEBUG_MMCHOST_SD   DEBUG_VERBOSE
+#define DEBUG_MMCHOST_SD_INFO  DEBUG_INFO
+#define DEBUG_MMCHOST_SD_ERROR DEBUG_ERROR
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL   *mFwProtocol;
+
+// Per Physical Layer Simplified Specs
+#ifndef NDEBUG
+STATIC CONST CHAR8* mStrSdState[] = { "idle", "ready", "ident", "stby",
+  "tran", "data", "rcv", "prg", "dis",
+  "ina" };
+STATIC CONST CHAR8 *mFsmState[] = { "identmode", "datamode", "readdata",
+"writedata", "readwait", "readcrc",
+"writecrc", "writewait1", "powerdown",
+"powerup", "writestart1", "writestart2",
+"genpulses", "writewait2", "?",
+"startpowdown" };
+#endif /* NDEBUG */
+STATIC UINT32 mLastGoodCmd = MMC_GET_INDX (MMC_CMD0);
+
+STATIC inline BOOLEAN
+IsAppCmd (
+  VOID
+  )
+{
+  return mLastGoodCmd == MMC_CMD55;
+}
+
+STATIC BOOLEAN
+IsBusyCmd (
+  IN  UINT32 MmcCmd
+  )
+{
+  if (IsAppCmd ()) {
+return FALSE;
+  }
+
+  return MmcCmd == MMC_CMD7 || MmcCmd == MMC_CMD12;
+}
+
+STATIC BOOLEAN
+IsWriteCmd (
+  IN  UINT32 MmcCmd
+  )
+{
+  if (IsAppCmd ()) {
+return FALSE;
+  }
+
+  return MmcCmd == MMC_CMD24 || MmcCmd == MMC_CMD25;
+}
+
+STATIC BOOLEAN
+IsReadCmd (
+  IN  UINT32 MmcCmd,
+  IN  UINT32 Argument
+  )
+{
+  if (MmcCmd == MMC_CMD8 && !IsAppCmd ()) {
+if (Argument == CMD8_MMC_ARG) {
+  DEBUG ((DEBUG_MMCHOST_SD, "Sending MMC CMD8 variant\n"));
+  return TRUE;
+} else {
+  ASSERT (Argument == CMD8_SD_ARG);
+  DEBUG ((DEBUG_MMCHOST_SD, "Sending SD CMD8 variant\n"));
+  return FALSE;
+}
+  }
+
+  return
+(MmcCmd == MMC_CMD6 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD17 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD18 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD13 && IsAppCmd ()) ||
+(MmcCmd == MMC_ACMD22 && IsAppCmd ()) ||
+(MmcCmd == MMC_ACMD51 && IsAppCmd ());
+}
+
+STATIC VOID
+SdHostDumpRegisters (
+  VOID
+  )
+{
+  DEBUG ((DEBUG_MMCHOST_SD, "SdHost: Registers Dump:\n"));
+  DEBUG ((DEBUG_MMCHOST_SD, "  CMD:  0x%8.8X\n", MmioRead32 (SDHOST_CMD)));
+  DEBUG ((

[edk2] [PATCH v5 edk2-platforms 14/22] Platform/RaspberryPi/RPi3: Add Arasan MMC driver

2019-02-05 Thread Pete Batard
The BCM283x family includes two SD card controllers, one being an Arasan
SDHCI compliant controller. This driver implements support for this one.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c   | 723 

 Platform/RaspberryPi/RPi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.h   |  50 
++
 Platform/RaspberryPi/RPi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.inf |  52 
++
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Sdio.h | 199 
++
 4 files changed, 1024 insertions(+)

diff --git 
a/Platform/RaspberryPi/RPi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c 
b/Platform/RaspberryPi/RPi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
new file mode 100644
index ..828b40f82a5f
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
@@ -0,0 +1,723 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 "ArasanMmcHostDxe.h"
+
+#define DEBUG_MMCHOST_SD DEBUG_VERBOSE
+
+BOOLEAN PreviousIsCardPresent = FALSE;
+UINT32 LastExecutedCommand = (UINT32) -1;
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+/**
+   These SD commands are optional, according to the SD Spec
+**/
+BOOLEAN
+IgnoreCommand (
+  UINT32 Command
+  )
+{
+  switch (Command) {
+  case MMC_CMD20:
+return TRUE;
+  default:
+return FALSE;
+  }
+}
+
+/**
+   Translates a generic SD command into the format used by the Arasan SD Host 
Controller
+**/
+UINT32
+TranslateCommand (
+  UINT32 Command,
+  UINT32 Argument
+  )
+{
+  UINT32 Translation = 0x;
+
+  if (LastExecutedCommand == CMD55) {
+switch (Command) {
+case MMC_CMD6:
+  Translation = ACMD6;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD6\n"));
+  break;
+case MMC_ACMD22:
+  Translation = ACMD22;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD22\n"));
+  break;
+case MMC_ACMD41:
+  Translation = ACMD41;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD41\n"));
+  break;
+case MMC_ACMD51:
+  Translation = ACMD51;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD51\n"));
+  break;
+default:
+  DEBUG ((DEBUG_ERROR, "ArasanMMCHost: TranslateCommand(): Unrecognized 
App command: %d\n", Command));
+}
+  } else {
+switch (Command) {
+case MMC_CMD0:
+  Translation = CMD0;
+  break;
+case MMC_CMD1:
+  Translation = CMD1;
+  break;
+case MMC_CMD2:
+  Translation = CMD2;
+  break;
+case MMC_CMD3:
+  Translation = CMD3;
+  break;
+case MMC_CMD5:
+  Translation = CMD5;
+  break;
+case MMC_CMD6:
+  Translation = CMD6;
+  break;
+case MMC_CMD7:
+  Translation = CMD7;
+  break;
+case MMC_CMD8: {
+  if (Argument == CMD8_SD_ARG) {
+Translation = CMD8_SD;
+DEBUG ((DEBUG_MMCHOST_SD, "Sending SD CMD8 variant\n"));
+  } else {
+ASSERT (Argument == CMD8_MMC_ARG);
+Translation = CMD8_MMC;
+DEBUG ((DEBUG_MMCHOST_SD, "Sending MMC CMD8 variant\n"));
+  }
+  break;
+}
+case MMC_CMD9:
+  Translation = CMD9;
+  break;
+case MMC_CMD11:
+  Translation = CMD11;
+  break;
+case MMC_CMD12:
+  Translation = CMD12;
+  break;
+case MMC_CMD13:
+  Translation = CMD13;
+  break;
+case MMC_CMD16:
+  Translation = CMD16;
+  break;
+case MMC_CMD17:
+  Translation = CMD17;
+  break;
+case MMC_CMD18:
+  Translation = CMD18;
+  break;
+case MMC_CMD23:
+  Translation = CMD23;
+  break;
+case MMC_CMD24:
+  Translation = CMD24;
+  break;
+case MMC_CMD25:
+  Translation = CMD25;
+  break;
+case MMC_CMD55:
+  Translation = CMD55;
+  break;
+default:
+  DEBUG ((DEBUG_ERROR, "ArasanMMCHost: TranslateCommand(): Unrecognized 
Command: %d\n", Command));
+}
+  }
+
+  return Translation;
+}
+
+/**
+   Repeatedly polls a register until its value becomes correct, or until 
MAX_RETRY_COUNT polls is reached
+**/
+EFI_STATUS
+PollRegisterWithMask (
+  IN UINTN Register,
+  IN UINTN Mask,
+  IN UINTN ExpectedValue
+  )
+{
+  UINTN RetryCount = 0;
+
+  while (RetryCount < MAX_RETRY_COUNT) {
+if ((MmioRead32 (Register) & Mask) != ExpectedValue) {
+  RetryCount++;
+  gBS->Stall (STALL_

[edk2] [PATCH v5 edk2-platforms 13/22] Platform/RaspberryPi/RPi3: Add base MMC driver

2019-02-05 Thread Pete Batard
This implements the MMC Host protocol, which is used by the two
concurrent SD interface drivers that the Raspberry Pi 3 supports.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/ComponentName.c | 163 
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/Diagnostics.c   | 263 ++
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/Mmc.c   | 460 +
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/Mmc.h   | 533 +++
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/MmcBlockIo.c| 469 ++
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/MmcDebug.c  | 170 
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/MmcDxe.inf  |  58 ++
 Platform/RaspberryPi/RPi3/Drivers/MmcDxe/MmcIdentification.c | 980 

 Platform/RaspberryPi/RPi3/Include/Protocol/RpiMmcHost.h  | 206 
 9 files changed, 3302 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/MmcDxe/ComponentName.c 
b/Platform/RaspberryPi/RPi3/Drivers/MmcDxe/ComponentName.c
new file mode 100644
index ..034da778cae2
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/MmcDxe/ComponentName.c
@@ -0,0 +1,163 @@
+/** @file
+ *
+ *  Component Name Protocol implementation for the MMC DXE driver
+ *
+ *  Copyright (c) 2011, ARM Limited. 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 "Mmc.h"
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL  gMmcComponentName = 
{
+  MmcGetDriverName,
+  MmcGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gMmcComponentName2 
= {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)MmcGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)MmcGetControllerName,
+  "en"
+};
+
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE
+mMmcDriverNameTable[] = {
+  {"eng;en", L"MMC/SD Card Interface Driver"},
+  {NULL,  NULL}
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+  @param  Language  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+  @param  DriverNameA pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+EFI_STATUS
+EFIAPI
+MmcGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+   Language,
+   This->SupportedLanguages,
+   mMmcDriverNameTable,
+   DriverName,
+   (BOOLEAN)(This == &gMmcComponentName)
+ );
+}
+
+/**
+  Retrieves a Unicode strin

[edk2] [PATCH v5 edk2-platforms 12/22] Platform/RaspberryPi/RPi3: Add Device Tree driver

2019-02-05 Thread Pete Batard
This driver serves the device tree that is either embedded
in the firmware volume or was provided through config.txt.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/FdtDxe/FdtDxe.c   | 364 
 Platform/RaspberryPi/RPi3/Drivers/FdtDxe/FdtDxe.inf |  53 +++
 2 files changed, 417 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/FdtDxe/FdtDxe.c 
b/Platform/RaspberryPi/RPi3/Drivers/FdtDxe/FdtDxe.c
new file mode 100644
index ..eb5698cb505b
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/FdtDxe/FdtDxe.c
@@ -0,0 +1,364 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrey Warkentin 
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+
+STATIC VOID *mFdtImage;
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL   *mFwProtocol;
+
+STATIC
+VOID
+UpdateMacAddress (
+  VOID
+  )
+{
+  INTN  Node;
+  INTN  Retval;
+  EFI_STATUSStatus;
+  UINT8 MacAddress[6];
+
+  //
+  // Locate the node that the 'ethernet' alias refers to
+  //
+  Node = fdt_path_offset (mFdtImage, "ethernet");
+  if (Node < 0) {
+DEBUG ((DEBUG_ERROR, "%a: failed to locate 'ethernet' alias\n", 
__FUNCTION__));
+return;
+  }
+
+  //
+  // Get the MAC address from the firmware
+  //
+  Status = mFwProtocol->GetMacAddress (MacAddress);
+  if (EFI_ERROR (Status)) {
+DEBUG ((DEBUG_ERROR, "%a: failed to retrieve MAC address\n", 
__FUNCTION__));
+return;
+  }
+
+  Retval = fdt_setprop (mFdtImage, Node, "mac-address", MacAddress,
+sizeof MacAddress);
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "%a: failed to create 'mac-address' property (%d)\n",
+  __FUNCTION__, Retval));
+return;
+  }
+
+  DEBUG ((DEBUG_INFO, "%a: setting MAC address to 
%02x:%02x:%02x:%02x:%02x:%02x\n",
+__FUNCTION__, MacAddress[0], MacAddress[1], MacAddress[2], MacAddress[3],
+MacAddress[4], MacAddress[5]));
+}
+
+STATIC
+VOID
+CleanMemoryNodes (
+  VOID
+  )
+{
+  INTN Node;
+  INT32 Retval;
+
+  Node = fdt_path_offset (mFdtImage, "/memory");
+  if (Node < 0) {
+return;
+  }
+
+  /*
+   * Remove bogus memory nodes which can make the booted
+   * OS go crazy and ignore the UEFI map.
+   */
+  DEBUG ((DEBUG_INFO, "Removing bogus /memory\n"));
+  Retval = fdt_del_node (mFdtImage, Node);
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Failed to remove /memory\n"));
+  }
+}
+
+STATIC
+VOID
+SanitizePSCI (
+  VOID
+  )
+{
+  INTN Node;
+  INTN Root;
+  INT32 Retval;
+
+  Root = fdt_path_offset (mFdtImage, "/");
+  ASSERT (Root >= 0);
+  if (Root < 0) {
+return;
+  }
+
+  Node = fdt_path_offset (mFdtImage, "/psci");
+  if (Node < 0) {
+Node = fdt_add_subnode (mFdtImage, Root, "psci");
+  }
+
+  ASSERT (Node >= 0);
+  if (Node < 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't find/create /psci\n"));
+return;
+  }
+
+  Retval = fdt_setprop_string (mFdtImage, Node, "compatible", "arm,psci-1.0");
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't set /psci compatible property\n"));
+return;
+  }
+
+  Retval = fdt_setprop_string (mFdtImage, Node, "method", "smc");
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't set /psci method property\n"));
+return;
+  }
+
+  Root = fdt_path_offset (mFdtImage, "/cpus");
+  if (Root < 0) {
+DEBUG ((DEBUG_ERROR, "No CPUs to update with PSCI enable-method?\n"));
+return;
+  }
+
+  Node = fdt_first_subnode (mFdtImage, Root);
+  while (Node >= 0) {
+if (fdt_setprop_string (mFdtImage, Node, "enable-method", "psci") != 0) {
+  DEBUG ((DEBUG_ERROR, "Failed to update enable-method for a CPU\n"));
+  return;
+}
+
+fdt_delprop (mFdtImage, Node, "cpu-release-addr");
+Node = fdt_next_subnode (mFdtImage, Node);
+  }
+}
+
+STATIC
+VOID
+CleanSimpleFramebuffer (
+  VOID
+  )
+{
+  INTN Node;
+  INT32 Retval;
+
+  /*
+   * Should look for nodes by kind and remove aliases
+   * by matching against device.
+   */
+  Node = fdt_path_offset (mFdtImage, "display0");
+  if (Node < 0) {
+return;
+  }
+
+  /*
+   *

[edk2] [PATCH v5 edk2-platforms 11/22] Platform/RaspberryPi/RPi3: Add NV storage driver

2019-02-05 Thread Pete Batard
Since the Raspberry Pi doesn't have a NVRAM, this driver is used to store
non-volatile user configuration settings into the firmware volume itself.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/FileIo.c   | 
196 
 Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/FvbInfo.c  | 
115 +++
 Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/VarBlockService.c  | 
971 
 Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/VarBlockService.h  | 
217 +
 Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.c   | 
331 +++
 Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.inf |  
93 ++
 6 files changed, 1923 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/FileIo.c 
b/Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/FileIo.c
new file mode 100644
index ..0e8cd516f65e
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/VarBlockServiceDxe/FileIo.c
@@ -0,0 +1,196 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2007-2009, Intel Corporation. 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 "VarBlockService.h"
+
+
+EFI_STATUS
+FileWrite (
+  IN EFI_FILE_PROTOCOL *File,
+  IN UINTN Offset,
+  IN UINTN Buffer,
+  IN UINTN Size
+  )
+{
+  EFI_STATUS Status;
+
+  Status = File->SetPosition (File, Offset);
+  ASSERT_EFI_ERROR (Status);
+  if (!EFI_ERROR (Status)) {
+Status = File->Write (File, &Size, (VOID*)Buffer);
+ASSERT_EFI_ERROR (Status);
+  }
+  return Status;
+}
+
+
+VOID
+FileClose (
+  IN  EFI_FILE_PROTOCOL *File
+  )
+{
+  File->Flush (File);
+  File->Close (File);
+}
+
+
+EFI_STATUS
+FileOpen (
+  IN  EFI_DEVICE_PATH_PROTOCOL *Device,
+  IN  CHAR16 *MappedFile,
+  OUT EFI_FILE_PROTOCOL **File,
+  IN  UINT64 OpenMode
+  )
+{
+  EFI_HANDLEHandle;
+  EFI_FILE_HANDLE   Root;
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL   *Volume;
+  EFI_STATUSStatus;
+
+  *File = NULL;
+
+  Status = gBS->LocateDevicePath (
+  &gEfiSimpleFileSystemProtocolGuid,
+  &Device,
+  &Handle
+);
+
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  Status = gBS->HandleProtocol (
+  Handle,
+  &gEfiSimpleFileSystemProtocolGuid,
+  (VOID**)&Volume
+);
+  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  //
+  // Open the root directory of the volume
+  //
+  Root = NULL;
+  Status = Volume->OpenVolume (
+ Volume,
+ &Root
+   );
+  ASSERT_EFI_ERROR (Status);
+  ASSERT (Root != NULL);
+
+  //
+  // Open file
+  //
+  Status = Root->Open (
+   Root,
+   File,
+   MappedFile,
+   OpenMode,
+   0
+ );
+  if (EFI_ERROR (Status)) {
+*File = NULL;
+  }
+
+  //
+  // Close the Root directory
+  //
+  Root->Close (Root);
+  return Status;
+}
+
+
+EFI_STATUS
+CheckStore (
+  IN  EFI_HANDLE SimpleFileSystemHandle,
+  OUT EFI_DEVICE_PATH_PROTOCOL **Device
+  )
+{
+  EFI_STATUS Status;
+  EFI_BLOCK_IO_PROTOCOL *BlkIo;
+  EFI_FILE_PROTOCOL *File;
+
+  *Device = NULL;
+  Status = gBS->HandleProtocol (
+  SimpleFileSystemHandle,
+  &gEfiBlockIoProtocolGuid,
+  (VOID*)&BlkIo
+);
+
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+  if (!BlkIo->Media->MediaPresent) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media not present!\n"));
+Status = EFI_NO_MEDIA;
+goto ErrHandle;
+  }
+  if (BlkIo->Media->ReadOnly) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media is read-only!\n"));
+Status = EFI_ACCESS_DENIED;
+goto ErrHandle;
+  }
+
+  Status = FileOpen (DevicePathFromHandle (SimpleFileSystemHandle),
+ mFvInstance->MappedFile, &File,
+ EFI_FILE_MODE_READ);
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+
+  /* We found it! Maybe do more checks...? */
+
+  FileClose (File);
+  *Device = DuplicateDevicePath (DevicePathFromHandle 
(SimpleFileSystemHandle));
+
+  ASSERT (*Device != NULL);
+
+ErrHandle:
+  return Status;
+}
+
+
+EFI_S

[edk2] [PATCH v5 edk2-platforms 10/22] Platform/RaspberryPi/RPi3: Add console driver

2019-02-05 Thread Pete Batard
Implements the graphic console (extended text output) needed
for user interaction.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/ComponentName.c   
  |  183 ++
 Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/GraphicsConsole.c 
  | 1837 
 Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/GraphicsConsole.h 
  |  591 +++
 Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxe.inf
  |   75 +
 Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxe.uni
  |   18 +
 
Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxeExtra.uni
 |   18 +
 Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/NewFont.c 
  |  287 +++
 Platform/RaspberryPi/RPi3/Include/Protocol/ExtendedTextOut.h   
  |   36 +
 8 files changed, 3045 insertions(+)

diff --git 
a/Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/ComponentName.c 
b/Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/ComponentName.c
new file mode 100644
index ..3ae639ad3d97
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/GraphicsConsoleDxe/ComponentName.c
@@ -0,0 +1,183 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2006-2016, Intel Corporation. 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 "GraphicsConsole.h"
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL  
gGraphicsConsoleComponentName = {
+  GraphicsConsoleComponentNameGetDriverName,
+  GraphicsConsoleComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL 
gGraphicsConsoleComponentName2 = {
+  
(EFI_COMPONENT_NAME2_GET_DRIVER_NAME)GraphicsConsoleComponentNameGetDriverName,
+  
(EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)GraphicsConsoleComponentNameGetControllerName,
+  "en"
+};
+
+
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE 
mGraphicsConsoleDriverNameTable[] = {
+  {
+"eng;en",
+(CHAR16*)L"Graphics Console Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+EFI_STATUS
+EFIAPI
+GraphicsConsoleComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+

[edk2] [PATCH v5 edk2-platforms 09/22] Platform/RaspberryPi/RPi3: Add display driver

2019-02-05 Thread Pete Batard
Implements the EFI_GRAPHICS_OUTPUT_PROTOCOL for the Raspberry
Pi platform, through framebuffer, including resolution querying
and screenshot support.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/ComponentName.c | 222 +++
 Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/DisplayDxe.c| 606 

 Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/DisplayDxe.h|  42 ++
 Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/DisplayDxe.inf  |  71 +++
 Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/Screenshot.c| 375 
 5 files changed, 1316 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/ComponentName.c 
b/Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/ComponentName.c
new file mode 100644
index ..9a84aea511f4
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/DisplayDxe/ComponentName.c
@@ -0,0 +1,222 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2006-2016, Intel Corporation. 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 "DisplayDxe.h"
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  );
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL *This,
+  IN  EFI_HANDLE  ControllerHandle,
+  IN  EFI_HANDLE  ChildHandle,
+  IN  CHAR8   *Language,
+  OUT CHAR16  **ControllerName
+  );
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
+  ComponentNameGetDriverName,
+  ComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)ComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)ComponentNameGetControllerName,
+  "en"
+};
+
+
+STATIC EFI_UNICODE_STRING_TABLE mDriverName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi Display Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mDeviceName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi Framebuffer"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by 

[edk2] [PATCH v5 edk2-platforms 08/22] Platform/RaspberryPi/RPi3: Add SMBIOS driver

2019-02-05 Thread Pete Batard
Static SMBIOS Table for the ARM platform, derived from EmulatorPkg.
Implements SMBIOS 2.7.1 required structures.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c   | 
903 
 Platform/RaspberryPi/RPi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.inf |  
56 ++
 2 files changed, 959 insertions(+)

diff --git 
a/Platform/RaspberryPi/RPi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c 
b/Platform/RaspberryPi/RPi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c
new file mode 100644
index ..7707763e4332
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c
@@ -0,0 +1,903 @@
+/** @file
+ *
+ *  Static SMBIOS Table for ARM platform
+ *  Derived from EmulatorPkg package
+ *
+ *  Note SMBIOS 2.7.1 Required structures:
+ *  BIOS Information (Type 0)
+ *  System Information (Type 1)
+ *  Board Information (Type 2)
+ *  System Enclosure (Type 3)
+ *  Processor Information (Type 4) - CPU Driver
+ *  Cache Information (Type 7) - For cache that is external to processor
+ *  System Slots (Type 9) - If system has slots
+ *  Physical Memory Array (Type 16)
+ *  Memory Device (Type 17) - For each socketed system-memory Device
+ *  Memory Array Mapped Address (Type 19) - One per contiguous block per 
Physical Memroy Array
+ *  System Boot Information (Type 32)
+ *
+ *  Copyright (c) 2017-2018, Andrey Warkentin 
+ *  Copyright (c) 2013, Linaro.org
+ *  Copyright (c) 2012, Apple Inc. All rights reserved.
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+/***
+SMBIOS data definition  TYPE0  BIOS Information
+/
+SMBIOS_TABLE_TYPE0 mBIOSInfoType0 = {
+  { EFI_SMBIOS_TYPE_BIOS_INFORMATION, sizeof (SMBIOS_TABLE_TYPE0), 0 },
+  1,// Vendor String
+  2,// BiosVersion String
+  0x0,  // BiosSegment
+  3,// BiosReleaseDate String
+  0x1F, // BiosSize
+  { // BiosCharacteristics
+0,//  Reserved  :2;  ///< Bits 0-1.
+0,//  Unknown   :1;
+0,//  BiosCharacteristicsNotSupported   :1;
+0,//  IsaIsSupported:1;
+0,//  McaIsSupported:1;
+0,//  EisaIsSupported   :1;
+0,//  PciIsSupported:1;
+0,//  PcmciaIsSupported :1;
+0,//  PlugAndPlayIsSupported:1;
+0,//  ApmIsSupported:1;
+0,//  BiosIsUpgradable  :1;
+0,//  BiosShadowingAllowed  :1;
+0,//  VlVesaIsSupported :1;
+0,//  EscdSupportIsAvailable:1;
+0,//  BootFromCdIsSupported :1;
+1,//  SelectableBootIsSupported :1;
+0,//  RomBiosIsSocketed :1;
+0,//  BootFromPcmciaIsSupported :1;
+0,//  EDDSpecificationIsSupported   :1;
+0,//  JapaneseNecFloppyIsSupported  :1;
+0,//  JapaneseToshibaFloppyIsSupported  :1;
+0,//  Floppy525_360IsSupported  :1;
+0,//  Floppy525_12IsSupported   :1;
+0,//  Floppy35_720IsSupported   :1;
+0,//  Floppy35_288IsSupported   :1;
+0,//  PrintScreenIsSupported:1;
+0,//  Keyboard8042IsSupported   :1;
+0,//  SerialIsSupported :1;
+0,//  PrinterIsSupported:1;
+0,//  CgaMonoIsSupported:1;
+0,//  NecPc98   :1;
+0 //  ReservedForVendor :32; ///< Bits 32-63. Bits 
32-47 reserved for BIOS vendor
+///< and bits 48-63 reserved for System Vendor.
+  },
+  {   // BIOSCharacteristicsExtensionBytes[]
+0x01, //  AcpiIsSupported   :1;
+  //  UsbLegacyIsSupported  :1;
+  //  AgpIsSupported:1;
+  //  I2OBootIsSupporte

[edk2] [PATCH v5 edk2-platforms 07/22] Platform/RaspberryPi/RPi3: Add platform config driver

2019-02-05 Thread Pete Batard
Provides the user-visible configuration options for the
firmware UI.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxe.c| 351 

 Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxe.inf  |  78 +
 Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxeFormSetGuid.h |  23 ++
 Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxeHii.uni   | 100 ++
 Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxeHii.vfr   | 306 
+
 5 files changed, 858 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxe.c 
b/Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxe.c
new file mode 100644
index ..b78d7deae402
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/ConfigDxe/ConfigDxe.c
@@ -0,0 +1,351 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "ConfigDxeFormSetGuid.h"
+
+extern UINT8 ConfigDxeHiiBin[];
+extern UINT8 ConfigDxeStrings[];
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+typedef struct {
+  VENDOR_DEVICE_PATH VendorDevicePath;
+  EFI_DEVICE_PATH_PROTOCOL End;
+} HII_VENDOR_DEVICE_PATH;
+
+STATIC HII_VENDOR_DEVICE_PATH mVendorDevicePath = {
+  {
+{
+  HARDWARE_DEVICE_PATH,
+  HW_VENDOR_DP,
+  {
+(UINT8)(sizeof (VENDOR_DEVICE_PATH)),
+(UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
+  }
+},
+CONFIGDXE_FORM_SET_GUID
+  },
+  {
+END_DEVICE_PATH_TYPE,
+END_ENTIRE_DEVICE_PATH_SUBTYPE,
+{
+  (UINT8)(END_DEVICE_PATH_LENGTH),
+  (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
+}
+  }
+};
+
+
+STATIC EFI_STATUS
+InstallHiiPages (
+  VOID
+  )
+{
+  EFI_STATUS Status;
+  EFI_HII_HANDLE HiiHandle;
+  EFI_HANDLE DriverHandle;
+
+  DriverHandle = NULL;
+  Status = gBS->InstallMultipleProtocolInterfaces (&DriverHandle,
+  &gEfiDevicePathProtocolGuid,
+  &mVendorDevicePath,
+  NULL);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  HiiHandle = HiiAddPackages (&gConfigDxeFormSetGuid,
+DriverHandle,
+ConfigDxeStrings,
+ConfigDxeHiiBin,
+NULL);
+
+  if (HiiHandle == NULL) {
+gBS->UninstallMultipleProtocolInterfaces (DriverHandle,
+   &gEfiDevicePathProtocolGuid,
+   &mVendorDevicePath,
+   NULL);
+return EFI_OUT_OF_RESOURCES;
+  }
+  return EFI_SUCCESS;
+}
+
+
+STATIC EFI_STATUS
+SetupVariables (
+  VOID
+  )
+{
+  UINTN Size;
+  UINT32 Var32;
+  EFI_STATUS Status;
+
+  /*
+   * Create the vars with default value.
+   * If we don't, forms won't be able to update.
+   */
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"CpuClock",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdCpuClock, PcdGet32 (PcdCpuClock));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"SdIsArasan",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdSdIsArasan, PcdGet32 (PcdSdIsArasan));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcDisableMulti",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcDisableMulti, PcdGet32 (PcdMmcDisableMulti));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcForce1Bit",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForce1Bit, PcdGet32 (PcdMmcForce1Bit));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcForceDefaultSpeed",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForceDefaultSpeed, PcdGet32 (PcdMmcForceDefaultSpeed));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcSdDefaultSpeedMHz",
+  &gConfigDxeFormSetGuid,
+   

[edk2] [PATCH v5 edk2-platforms 06/22] Platform/RaspberryPi/RPi3: Add firmware driver

2019-02-05 Thread Pete Batard
Implements the base driver that is used to provide or set platform
specific configuration data.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 

Reviewed-by: Ard Biesheuvel 
---
 Platform/RaspberryPi/RPi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c   | 1084 

 Platform/RaspberryPi/RPi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.inf |   50 +
 Platform/RaspberryPi/RPi3/Include/Protocol/RpiFirmware.h|  131 +++
 3 files changed, 1265 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c 
b/Platform/RaspberryPi/RPi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
new file mode 100644
index ..d330e45fdc4c
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
@@ -0,0 +1,1084 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrei Warkentin 
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include 
+
+//
+// The number of statically allocated buffer pages
+//
+#define NUM_PAGES   1
+
+//
+// The number of iterations to perform when waiting for the mailbox
+// status to change
+//
+#define MAX_TRIES   0x10
+
+STATIC VOID  *mDmaBuffer;
+STATIC VOID  *mDmaBufferMapping;
+STATIC UINTN mDmaBufferBusAddress;
+
+STATIC SPIN_LOCK mMailboxLock;
+
+STATIC
+BOOLEAN
+DrainMailbox (
+  VOID
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if (Val & (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+BOOLEAN
+MailboxWaitForStatusCleared (
+  IN  UINTN   StatusMask
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if ((Val & StatusMask) == 0) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+EFI_STATUS
+MailboxTransaction (
+  INUINTN   Length,
+  INUINTN   Channel,
+  OUT   UINT32  *Result
+  )
+{
+  if (Channel >= BCM2836_MBOX_NUM_CHANNELS) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  if (!DrainMailbox ()) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for mailbox to drain\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Wait for the 'output register full' bit to become clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_FULL)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for outbox to become empty\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Start the mailbox transaction
+  //
+  MmioWrite32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_WRITE_OFFSET,
+(UINT32)((UINTN)mDmaBufferBusAddress | Channel));
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Wait for the 'input register empty' bit to clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for inbox to become full\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Read back the result
+  //
+  ArmDataSynchronizationBarrier ();
+  *Result = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  ArmDataSynchronizationBarrier ();
+
+  return EFI_SUCCESS;
+}
+
+#pragma pack(1)
+typedef struct {
+  UINT32BufferSize;
+  UINT32Response;
+} RPI_FW_BUFFER_HEAD;
+
+typedef struct {
+  UINT32TagId;
+  UINT32TagSize;
+  UINT32TagValueSize;
+} RPI_FW_TAG_HEAD;
+
+typedef struct {
+  UINT32DeviceId;
+  UINT32PowerState;
+} RPI_FW_POWER_STATE_TAG;
+
+typedef struct {
+  RPI_FW_BUFFER_HEADBufferHead;
+  RPI_FW_TAG_HEAD   TagHead;
+  RPI_FW_POWER_STATE_TAGTagBody;
+  UINT32EndTag;
+} RPI_FW_SET_POWER_STATE_CMD;
+#pragma pack()
+
+STATIC
+EFI_STATUS
+EFIAPI
+RpiFirmwa

[edk2] [PATCH v5 edk2-platforms 05/22] Platform/RaspberryPi/RPi3: Add platform library

2019-02-05 Thread Pete Batard
A platform helper library, relying on low level Mailbox messaging
between the CPU and VideoCore to obtain current platform information,
that is used by various services and drivers.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Include/IndustryStandard/RpiMbox.h  | 
108 +
 Platform/RaspberryPi/RPi3/Library/PlatformLib/AArch64/RaspberryPiHelper.S | 
107 +
 Platform/RaspberryPi/RPi3/Library/PlatformLib/PlatformLib.inf |  
65 
 Platform/RaspberryPi/RPi3/Library/PlatformLib/RaspberryPi.c   |  
99 
 Platform/RaspberryPi/RPi3/Library/PlatformLib/RaspberryPiMem.c| 
160 
 5 files changed, 539 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/Include/IndustryStandard/RpiMbox.h 
b/Platform/RaspberryPi/RPi3/Include/IndustryStandard/RpiMbox.h
new file mode 100644
index ..8547ad05ba61
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Include/IndustryStandard/RpiMbox.h
@@ -0,0 +1,108 @@
+/** @file
+ *
+ * Copyright (c) 2019, Pete Batard 
+ * Copyright (c) 2016, Linaro Limited. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * Neither the name of ARM nor the names of its contributors may be used
+ * to endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **/
+
+#ifndef __RASPBERRY_PI_MAILBOX_H__
+#define __RASPBERRY_PI_MAILBOX_H__
+
+/* Mailbox channels */
+#define RPI_MBOX_PM_CHANNEL   0
+#define RPI_MBOX_FB_CHANNEL   1
+#define RPI_MBOX_VUART_CHANNEL2
+#define RPI_MBOX_VCHIQ_CHANNEL3
+#define RPI_MBOX_LED_CHANNEL  4
+#define RPI_MBOX_BUTTON_CHANNEL   5
+#define RPI_MBOX_TOUCHSCREEN_CHANNEL  6
+/* Request from ARM for response by VideoCore */
+#define RPI_MBOX_VC_CHANNEL   8
+/* Request from VideoCore for response by ARM */
+#define RPI_MBOX_ARM_CHANNEL  9
+
+#define RPI_MBOX_RESP_SUCCESS 0x8000
+#define RPI_MBOX_RESP_FAILURE 0x8001
+
+#define RPI_MBOX_VALUE_SIZE_RESPONSE_MASK BIT31
+
+#define RPI_MBOX_GET_REVISION 0x0001
+#define RPI_MBOX_GET_BOARD_MODEL  0x00010001
+#define RPI_MBOX_GET_BOARD_REVISION   0x00010002
+#define RPI_MBOX_GET_MAC_ADDRESS  0x00010003
+#define RPI_MBOX_GET_BOARD_SERIAL 0x00010004
+#define RPI_MBOX_GET_ARM_MEMSIZE  0x00010005
+
+#define RPI_MBOX_SET_POWER_STATE  0x00028001
+
+#define RPI_MBOX_POWER_STATE_SDHCI0x
+#define RPI_MBOX_POWER_STATE_UART00x0001
+#define RPI_MBOX_POWER_STATE_UART10x0002
+#define RPI_MBOX_POWER_STATE_USB_HCD  0x0003
+#define RPI_MBOX_POWER_STATE_I2C0 0x0004
+#define RPI_MBOX_POWER_STATE_I2C1 0x0005
+#define RPI_MBOX_POWER_STATE_I2C2 0x0006
+#define RPI_MBOX_POWER_STATE_SPI  0x0007
+#define RPI_MBOX_POWER_STATE_CCP2TX   0x0008
+
+#define RPI_MBOX_GET_CLOCK_RATE   0x0003000

[edk2] [PATCH v5 edk2-platforms 03/22] Platform/RaspberryPi/RPi3: Add ACPI tables

2019-02-05 Thread Pete Batard
These ACPI tables were mostly derived or copied from the MS-IoT ones.

This means that they are targetting Windows OSes, rather than Linux ones,
and aren't ACPI compliant, especially when it comes to their descriptors.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/AcpiTables/AcpiTables.h   |  82 
 Platform/RaspberryPi/RPi3/AcpiTables/AcpiTables.inf |  46 ++
 Platform/RaspberryPi/RPi3/AcpiTables/Csrt.aslc  | 332 +
 Platform/RaspberryPi/RPi3/AcpiTables/Dbg2.aslc  |  34 ++
 Platform/RaspberryPi/RPi3/AcpiTables/Dsdt.asl   | 511 
 Platform/RaspberryPi/RPi3/AcpiTables/Fadt.aslc  |  52 ++
 Platform/RaspberryPi/RPi3/AcpiTables/Gtdt.aslc  |  33 ++
 Platform/RaspberryPi/RPi3/AcpiTables/Madt.aslc  |  62 +++
 Platform/RaspberryPi/RPi3/AcpiTables/Pep.asl|  95 
 Platform/RaspberryPi/RPi3/AcpiTables/Pep.c  |  84 
 Platform/RaspberryPi/RPi3/AcpiTables/Pep.h  | 126 +
 Platform/RaspberryPi/RPi3/AcpiTables/Rhpx.asl   | 201 
 Platform/RaspberryPi/RPi3/AcpiTables/Sdhc.asl   | 105 
 Platform/RaspberryPi/RPi3/AcpiTables/Spcr.asl   |  53 ++
 Platform/RaspberryPi/RPi3/AcpiTables/Uart.asl   | 158 ++
 15 files changed, 1974 insertions(+)

diff --git a/Platform/RaspberryPi/RPi3/AcpiTables/AcpiTables.h 
b/Platform/RaspberryPi/RPi3/AcpiTables/AcpiTables.h
new file mode 100644
index ..be28b6decefd
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/AcpiTables/AcpiTables.h
@@ -0,0 +1,82 @@
+/** @file
+ *
+ *  RPi3 defines for constructing ACPI tables
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ **/
+
+#ifndef __ACPITABLES_H__
+#define __ACPITABLES_H__
+
+#include 
+
+#define EFI_ACPI_OEM_ID   {'M','C','R','S','F','T'} // 
OEMID 6 bytes long
+#define EFI_ACPI_OEM_TABLE_ID SIGNATURE_64 
('R','P','I','3','E','D','K','2') // OEM table id 8 bytes long
+#define EFI_ACPI_OEM_REVISION 0x02000820
+#define EFI_ACPI_CREATOR_ID   SIGNATURE_32 ('R','P','I','3')
+#define EFI_ACPI_CREATOR_REVISION 0x0097
+
+#define EFI_ACPI_VENDOR_IDSIGNATURE_32 ('M','S','F','T')
+#define EFI_ACPI_CSRT_REVISION0x0005
+#define EFI_ACPI_CSRT_DEVICE_ID_DMA   0x0009 // Fixed id
+#define EFI_ACPI_CSRT_RESOURCE_ID_IN_DMA_GRP  0x0 // Count up from 0
+
+#define RPI3_DMA_CHANNEL_COUNT10 // All 10 DMA channels are 
listed, including the reserved ones
+#define RPI3_DMA_USED_CHANNEL_COUNT   5  // Use 5 DMA channels
+
+#define EFI_ACPI_5_0_CSRT_REVISION0x
+
+typedef enum
+{
+  EFI_ACPI_CSRT_RESOURCE_TYPE_RESERVED,   // 0
+  EFI_ACPI_CSRT_RESOURCE_TYPE_INTERRUPT,  // 1
+  EFI_ACPI_CSRT_RESOURCE_TYPE_TIMER,  // 2
+  EFI_ACPI_CSRT_RESOURCE_TYPE_DMA,// 3
+  EFI_ACPI_CSRT_RESOURCE_TYPE_CACHE,  // 4
+}
+CSRT_RESOURCE_TYPE;
+
+typedef enum
+{
+  EFI_ACPI_CSRT_RESOURCE_SUBTYPE_DMA_CHANNEL, // 0
+  EFI_ACPI_CSRT_RESOURCE_SUBTYPE_DMA_CONTROLLER   // 1
+}
+CSRT_DMA_SUBTYPE;
+
+//
+// CSRT Resource Group header 24 bytes long
+//
+typedef struct
+{
+  UINT32 Length;  // Length
+  UINT32 VendorID;// 4 bytes
+  UINT32 SubVendorId; // 4 bytes
+  UINT16 DeviceId;// 2 bytes
+  UINT16 SubdeviceId; // 2 bytes
+  UINT16 Revision;// 2 bytes
+  UINT16 Reserved;// 2 bytes
+  UINT32 SharedInfoLength;// 4 bytes
+} EFI_ACPI_5_0_CSRT_RESOURCE_GROUP_HEADER;
+
+//
+// CSRT Resource Descriptor 12 bytes total
+//
+typedef struct
+{
+  UINT32 Length;  // 4 bytes
+  UINT16 ResourceType;// 2 bytes
+  UINT16 ResourceSubType; // 2 bytes
+  UINT32 UID; // 4 bytes
+} EFI_ACPI_5_0_CSRT_RESOURCE_D

[edk2] [PATCH v5 edk2-platforms 04/22] Platform/RaspberryPi/RPi3: Add reset and memory init libraries

2019-02-05 Thread Pete Batard
The memory init library ensures that relevant memory regions are
reserved on boot.

The reset library supports the ResetSystem runtime call using PSCI
and signals the gRaspberryPiEventResetGuid event group on reset,
which we need to save modified configuration vars to NVRAM.

Note that we tried to drop ResetLib altogether, and use the reset
notification event provided by the EDK2, but this results in
Linux kernel panics, for which we have no workaround.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/RaspberryPi/RPi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c   | 162 

 Platform/RaspberryPi/RPi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf |  51 
++
 Platform/RaspberryPi/RPi3/Library/ResetLib/ResetLib.c   | 104 
+
 Platform/RaspberryPi/RPi3/Library/ResetLib/ResetLib.inf |  46 
++
 4 files changed, 363 insertions(+)

diff --git 
a/Platform/RaspberryPi/RPi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c 
b/Platform/RaspberryPi/RPi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
new file mode 100644
index ..903364e08b15
--- /dev/null
+++ b/Platform/RaspberryPi/RPi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
@@ -0,0 +1,162 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrey Warkentin 
+ *  Copyright (c) 2011-2015, ARM Limited. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+extern UINT64 mSystemMemoryEnd;
+
+VOID
+BuildMemoryTypeInformationHob (
+  VOID
+  );
+
+STATIC
+VOID
+InitMmu (
+  IN ARM_MEMORY_REGION_DESCRIPTOR  *MemoryTable
+  )
+{
+  RETURN_STATUS Status;
+
+  //Note: Because we called PeiServicesInstallPeiMemory() before to call 
InitMmu() the MMU Page Table
+  //  resides in DRAM (even at the top of DRAM as it is the first 
permanent memory allocation)
+  Status = ArmConfigureMmu (MemoryTable, NULL, NULL);
+  if (EFI_ERROR (Status)) {
+DEBUG ((DEBUG_ERROR, "Error: Failed to enable MMU\n"));
+  }
+}
+
+STATIC
+VOID
+AddRuntimeServicesRegion (
+  IN ARM_MEMORY_REGION_DESCRIPTOR *Desc
+)
+{
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+Desc->PhysicalBase,
+Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiRuntimeServicesData
+  );
+}
+
+STATIC
+VOID
+AddReservedMemoryRegion (
+  IN ARM_MEMORY_REGION_DESCRIPTOR *Desc
+  )
+{
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+Desc->PhysicalBase,
+Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiReservedMemoryType
+  );
+}
+
+/*++
+
+Routine Description:
+
+
+
+Arguments:
+
+  FileHandle  - Handle of the file being invoked.
+  PeiServices - Describes the list of possible PEI Services.
+
+Returns:
+
+  Status -  EFI_SUCCESS if the boot mode could be set
+
+--*/
+EFI_STATUS
+EFIAPI
+MemoryPeim (
+  IN EFI_PHYSICAL_ADDRESS  UefiMemoryBase,
+  IN UINT64UefiMemorySize
+  )
+{
+  ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;
+
+  // Get Virtual Memory Map from the Platform Library
+  ArmPlatformGetVirtualMemoryMap (&MemoryTable);
+
+  // Ensure PcdSystemMemorySize has been set
+  ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);
+
+  // FD without variable store
+  AddReservedMemoryRegion (&MemoryTable[0]);
+
+  // Variable store.
+  AddRuntimeServicesRegion (&MemoryTable[1]);
+
+  // Trusted Firmware region
+  AddReservedMemoryRegion (&MemoryTable[2]);
+
+  // Usable memory.
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+MemoryTable[3].PhysicalBase,
+MemoryTable[

[edk2] [PATCH v5 edk2-platforms 02/22] Silicon/Broadcom/Bcm283x: Add GpioLib

2019-02-05 Thread Pete Batard
This library is meant to be used by Bcm283x-based platforms, such
as the Raspberry Pi, to control the GPIO port pins on said platform.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h | 49 
+++
 Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h  | 33 
 Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c  | 89 

 Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf| 39 +
 4 files changed, 210 insertions(+)

diff --git a/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h 
b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h
new file mode 100644
index ..5fc43ddaa27b
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h
@@ -0,0 +1,49 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ **/
+
+#ifndef __BCM2836_GPIO_H__
+#define __BCM2836_GPIO_H__
+
+#define GPIO_BASE_ADDRESS  (BCM2836_SOC_REGISTERS + 0x0020)
+
+#define GPIO_GPFSEL0   (GPIO_BASE_ADDRESS + 0x00)
+#define GPIO_GPFSEL1   (GPIO_BASE_ADDRESS + 0x04)
+#define GPIO_GPFSEL2   (GPIO_BASE_ADDRESS + 0x08)
+#define GPIO_GPFSEL3   (GPIO_BASE_ADDRESS + 0x0C)
+#define GPIO_GPFSEL4   (GPIO_BASE_ADDRESS + 0x10)
+#define GPIO_GPFSEL5   (GPIO_BASE_ADDRESS + 0x14)
+
+#define GPIO_GPCLR0(GPIO_BASE_ADDRESS + 0x28)
+#define GPIO_GPCLR1(GPIO_BASE_ADDRESS + 0x2C)
+
+#define GPIO_GPSET0(GPIO_BASE_ADDRESS + 0x1C)
+#define GPIO_GPSET1(GPIO_BASE_ADDRESS + 0x20)
+
+#define GPIO_FSEL_INPUT0x0
+#define GPIO_FSEL_OUTPUT   0x1
+#define GPIO_FSEL_ALT0 0x4
+#define GPIO_FSEL_ALT1 0x5
+#define GPIO_FSEL_ALT2 0x6
+#define GPIO_FSEL_ALT3 0x7
+#define GPIO_FSEL_ALT4 0x3
+#define GPIO_FSEL_ALT5 0x2
+
+#define GPIO_FSEL_PINS_PER_REGISTER 10
+#define GPIO_FSEL_BITS_PER_PIN  3
+#define GPIO_FSEL_MASK  ((1 << GPIO_FSEL_BITS_PER_PIN) - 1)
+
+#define GPIO_PINS  54
+
+#endif /* __BCM2836_GPIO_H__ */
diff --git a/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h 
b/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h
new file mode 100644
index ..c3e1fc21bf8d
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h
@@ -0,0 +1,33 @@
+/** @file
+ *
+ *  GPIO manipulation.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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.
+ *
+ **/
+
+#ifndef __GPIO_LIB__
+#define __GPIO_LIB__
+
+#include 
+
+VOID
+GpioPinFuncSet (
+  IN  UINTN Pin,
+  IN  UINTN Function
+  );
+
+UINTN
+GpioPinFuncGet (
+  IN  UINTN Pin
+  );
+
+#endif /* __GPIO_LIB__ */
diff --git a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c 
b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c
new file mode 100644
index ..8cf560e4fcc5
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c
@@ -0,0 +1,89 @@
+/** @file
+ *
+ *  GPIO manipulation.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC
+VOID
+GpioFSELModify (
+  IN  UINTN RegIndex,
+  IN  UINT32 ModifyMask,
+  IN  UINT32 FunctionMask
+  )
+{
+  UINT32 Val;
+  EFI_PHYSICAL_ADDRESS Reg;
+
+  Reg = RegIndex * sizeof (UINT32) + GPIO_GPFSEL0;
+
+  ASSERT (Reg <= GPIO_GPFSEL5);
+  ASSERT ((~ModifyMask & FunctionMask) == 0);
+
+  Val = MmioRead32 (Reg);
+  Val &= ~ModifyMask;
+  Val |= FunctionMask;
+  MmioWrite32 (Reg, Val);
+}
+
+VOID
+GpioPinFu

[edk2] [PATCH v5 edk2-platforms 01/22] Silicon/Broadcom/Bcm283x: Add interrupt driver

2019-02-05 Thread Pete Batard
This currently only implements support for the architected timer
interrupts on the per-CPU interrupt controllers.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 

Reviewed-by: Ard Biesheuvel 
---
 Silicon/Broadcom/Bcm283x/Bcm283x.dec   |  23 ++
 Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c   | 367 

 Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.inf |  48 +++
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836.h|  72 
 4 files changed, 510 insertions(+)

diff --git a/Silicon/Broadcom/Bcm283x/Bcm283x.dec 
b/Silicon/Broadcom/Bcm283x/Bcm283x.dec
new file mode 100644
index ..d193da4c0e1e
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Bcm283x.dec
@@ -0,0 +1,23 @@
+## @file
+#
+#  Copyright (c) 2019, Pete Batard 
+#
+#  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.
+#
+##
+
+[Defines]
+  DEC_SPECIFICATION  = 0x0001001A
+  PACKAGE_NAME   = Bcm283xPkg
+  PACKAGE_GUID   = 900C0F44-1152-4FF9-B9C5-933E2918C831
+  PACKAGE_VERSION= 1.0
+
+[Includes]
+  Include
diff --git a/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c 
b/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c
new file mode 100644
index ..9058aa94ffb9
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c
@@ -0,0 +1,367 @@
+/** @file
+ *
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+//
+// This currently only implements support for the architected timer interrupts
+// on the per-CPU interrupt controllers.
+//
+#define NUM_IRQS(4)
+
+#ifdef MDE_CPU_AARCH64
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_AARCH64_IRQ
+#else
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_ARM_IRQ
+#endif
+
+STATIC CONST
+EFI_PHYSICAL_ADDRESS RegBase = FixedPcdGet32 (PcdInterruptBaseAddress);
+
+//
+// Notifications
+//
+STATIC EFI_EVENTmExitBootServicesEvent;
+STATIC HARDWARE_INTERRUPT_HANDLER   mRegisteredInterruptHandlers[NUM_IRQS];
+
+/**
+  Shutdown our hardware
+
+  DXE Core will disable interrupts and turn off the timer and disable 
interrupts
+  after all the event handlers have run.
+
+  @param[in]  Event   The Event that is being processed
+  @param[in]  Context Event Context
+**/
+STATIC
+VOID
+EFIAPI
+ExitBootServicesEvent (
+  IN EFI_EVENT  Event,
+  IN VOID   *Context
+  )
+{
+  // Disable all interrupts
+  MmioWrite32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 0);
+}
+
+/**
+  Enable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt enabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+EnableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT (FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioOr32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 1 << Source);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Disable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt disabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+DisableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT (FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioAnd32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, ~(1 << Source));
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Register Handler for the specified interrupt source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+  @param Handler  Call

[edk2] [PATCH v5 edk2-platforms 00/22] Platform/RaspberryPi: Add Raspberry Pi 3 support

2019-02-05 Thread Pete Batard
Changes for v5:

* Raspberry/Pi3 -> RaspberryPi/RPi3
* Remove VirtualRealTimeClockLib as well as BUILD_EPOCH macro (use the upcoming
  EmbeddedPkg Virtual RTC from EDK2 instead)
* Use $(PLATFORM_NAME) where possible in .dsc and .fdf
* Update Readme to remove build instructions, describe ACPI limitations, fix
  ATF Readme link and split OS installation & test notes into a separate file.
* Add -Wl,--fix-cortex-a53-843419 to LINK_FLAGS

IMPORTANT: Due to the removal of VirtualRealTimeClockLib this series requires
https://lists.01.org/pipermail/edk2-devel/2019-February/036301.html to have
been applied to your edk2 repository.

Regards,

/Pete


Pete Batard (22):
  Silicon/Broadcom/Bcm283x: Add interrupt driver
  Silicon/Broadcom/Bcm283x: Add GpioLib
  Platform/RaspberryPi/RPi3: Add ACPI tables
  Platform/RaspberryPi/RPi3: Add reset and memory init libraries
  Platform/RaspberryPi/RPi3: Add platform library
  Platform/RaspberryPi/RPi3: Add firmware driver
  Platform/RaspberryPi/RPi3: Add platform config driver
  Platform/RaspberryPi/RPi3: Add SMBIOS driver
  Platform/RaspberryPi/RPi3: Add display driver
  Platform/RaspberryPi/RPi3: Add console driver
  Platform/RaspberryPi/RPi3: Add NV storage driver
  Platform/RaspberryPi/RPi3: Add Device Tree driver
  Platform/RaspberryPi/RPi3: Add base MMC driver
  Platform/RaspberryPi/RPi3: Add Arasan MMC driver
  Platform/RaspberryPi/RPi3: Add SD Host driver
  Platform/RaspberryPi/RPi3: Add platform boot manager and helper libs
  Platform/RaspberryPi/RPi3: Add USB host driver
  Platform/RaspberryPi/RPi3 *NON-OSI*: Add ATF binaries
  Platform/RaspberryPi/RPi3 *NON-OSI*: Add Device Tree binaries
  Platform/RaspberryPi/RPi3 *NON-OSI*: Add logo driver
  Platform/RaspberryPi/RPi3: Add platform
  Platform/RaspberryPi/RPi3: Add platform readme's

 .../RaspberryPi/RPi3/AcpiTables/AcpiTables.h  |   82 +
 .../RPi3/AcpiTables/AcpiTables.inf|   46 +
 .../RaspberryPi/RPi3/AcpiTables/Csrt.aslc |  332 +++
 .../RaspberryPi/RPi3/AcpiTables/Dbg2.aslc |   34 +
 Platform/RaspberryPi/RPi3/AcpiTables/Dsdt.asl |  511 +
 .../RaspberryPi/RPi3/AcpiTables/Fadt.aslc |   52 +
 .../RaspberryPi/RPi3/AcpiTables/Gtdt.aslc |   33 +
 .../RaspberryPi/RPi3/AcpiTables/Madt.aslc |   62 +
 Platform/RaspberryPi/RPi3/AcpiTables/Pep.asl  |   95 +
 Platform/RaspberryPi/RPi3/AcpiTables/Pep.c|   84 +
 Platform/RaspberryPi/RPi3/AcpiTables/Pep.h|  126 ++
 Platform/RaspberryPi/RPi3/AcpiTables/Rhpx.asl |  201 ++
 Platform/RaspberryPi/RPi3/AcpiTables/Sdhc.asl |  105 +
 Platform/RaspberryPi/RPi3/AcpiTables/Spcr.asl |   53 +
 Platform/RaspberryPi/RPi3/AcpiTables/Uart.asl |  158 ++
 .../RaspberryPi/RPi3/DeviceTree/License.txt   |  340 +++
 .../RPi3/DeviceTree/bcm2710-rpi-3-b-plus.dtb  |  Bin 0 -> 25617 bytes
 .../RPi3/DeviceTree/bcm2710-rpi-3-b-plus.dts  | 1263 
 .../RPi3/DeviceTree/bcm2710-rpi-3-b.dtb   |  Bin 0 -> 25354 bytes
 .../RPi3/DeviceTree/bcm2710-rpi-3-b.dts   | 1259 +++
 .../ArasanMmcHostDxe/ArasanMmcHostDxe.c   |  723 +++
 .../ArasanMmcHostDxe/ArasanMmcHostDxe.h   |   50 +
 .../ArasanMmcHostDxe/ArasanMmcHostDxe.inf |   52 +
 .../RPi3/Drivers/ConfigDxe/ConfigDxe.c|  351 
 .../RPi3/Drivers/ConfigDxe/ConfigDxe.inf  |   78 +
 .../Drivers/ConfigDxe/ConfigDxeFormSetGuid.h  |   23 +
 .../RPi3/Drivers/ConfigDxe/ConfigDxeHii.uni   |  100 +
 .../RPi3/Drivers/ConfigDxe/ConfigDxeHii.vfr   |  306 +++
 .../RPi3/Drivers/DisplayDxe/ComponentName.c   |  222 ++
 .../RPi3/Drivers/DisplayDxe/DisplayDxe.c  |  606 ++
 .../RPi3/Drivers/DisplayDxe/DisplayDxe.h  |   42 +
 .../RPi3/Drivers/DisplayDxe/DisplayDxe.inf|   71 +
 .../RPi3/Drivers/DisplayDxe/Screenshot.c  |  375 
 .../RPi3/Drivers/DwUsbHostDxe/ComponentName.c |  225 ++
 .../RPi3/Drivers/DwUsbHostDxe/DriverBinding.c |  274 +++
 .../RPi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.c  | 1635 +++
 .../RPi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.h  |  162 ++
 .../Drivers/DwUsbHostDxe/DwUsbHostDxe.inf |   59 +
 .../RPi3/Drivers/DwUsbHostDxe/DwcHw.h |  788 +++
 .../RaspberryPi/RPi3/Drivers/FdtDxe/FdtDxe.c  |  364 
 .../RPi3/Drivers/FdtDxe/FdtDxe.inf|   53 +
 .../GraphicsConsoleDxe/ComponentName.c|  183 ++
 .../GraphicsConsoleDxe/GraphicsConsole.c  | 1837 +
 .../GraphicsConsoleDxe/GraphicsConsole.h  |  591 ++
 .../GraphicsConsoleDxe/GraphicsConsoleDxe.inf |   75 +
 .../GraphicsConsoleDxe/GraphicsConsoleDxe.uni |   18 +
 .../GraphicsConsoleDxeExtra.uni   |   18 +
 .../RPi3/Drivers/GraphicsConsoleDxe/NewFont.c |  287 +++
 .../RPi3/Drivers/LogoDxe/License.txt  |   10 +
 .../RaspberryPi/RPi3/Drivers/LogoDxe/Logo.bmp |  Bin 0 -> 185398 bytes
 .../RaspberryPi/RPi3/Drivers/LogoDxe/Logo.c   |  159 ++
 .../RaspberryPi/RPi3/Drivers/LogoDxe/Logo.eps |  Bin 0 -> 250982 bytes
 .../RaspberryPi/RPi3/Drivers/LogoDxe/Logo.idf |   18 +
 .../RaspberryPi/RPi3/Drivers

[edk2] [PATCH 1/1] EmbeddedPkg/Library: Add VirtualRealTimeClockLib

2019-02-04 Thread Pete Batard
This is designed to be used on platforms where a a real RTC is not
available and relies on an RtcEpochSeconds variable having been set or,
if that is not the case, falls back to using the epoch embedded at
compilation time.

Note that, in order to keep things simple for the setting of the
compilation time variable, only GCC environments with UNIX-like shells
and where a 'date' command is available are meant to be supported for
now.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c   | 400 

 EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf |  43 
+++
 2 files changed, 443 insertions(+)

diff --git 
a/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c 
b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
new file mode 100644
index ..4c354730d02b
--- /dev/null
+++ b/EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
@@ -0,0 +1,400 @@
+/** @file
+ *
+ *  Implement virtual EFI RealTimeClock runtime services.
+ *
+ *  Coypright (c) 2019, Pete Batard 
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2011-2014, ARM Ltd. All rights reserved.
+ *  Copyright (c) 2008-2010, Apple Inc. All rights reserved.
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ *  Based on 
ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
+ *
+ **/
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC CONST CHAR16  mEpochVariableName[] = L"RtcEpochSeconds";
+STATIC CONST CHAR16  mTimeZoneVariableName[]  = L"RtcTimeZone";
+STATIC CONST CHAR16  mDaylightVariableName[]  = L"RtcDaylight";
+
+/**
+   Returns the current time and date information, and the time-keeping 
capabilities
+   of the virtual RTC.
+
+   @param  Time  A pointer to storage to receive a snapshot of 
the current time.
+   @param  Capabilities  An optional pointer to a buffer to receive 
the real time clock
+ device's capabilities.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER Time is NULL.
+   @retval EFI_DEVICE_ERROR  The time could not be retrieved due to 
hardware error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibGetTime (
+  OUT EFI_TIME   *Time,
+  OUT EFI_TIME_CAPABILITIES  *Capabilities
+  )
+{
+  EFI_STATUS  Status;
+  UINT32  EpochSeconds;
+  INT16   TimeZone;
+  UINT8   Daylight;
+  UINT64  Freq;
+  UINT64  Counter;
+  UINT64  Remainder;
+  UINTN   ElapsedSeconds;
+  UINTN   Size;
+
+  if (Time == NULL) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  // Get the counter frequency
+  Freq = GetPerformanceCounterProperties (NULL, NULL);
+  if (Freq == 0) {
+return EFI_DEVICE_ERROR;
+  }
+
+  // Get the epoch time from non-volatile storage
+  Size = sizeof (UINTN);
+  ElapsedSeconds = 0;
+  Status = EfiGetVariable (
+ (CHAR16 *)mEpochVariableName,
+ &gEfiCallerIdGuid,
+ NULL,
+ &Size,
+ (VOID *)&ElapsedSeconds
+ );
+  // Fall back to compilation-time epoch if not set
+  if (EFI_ERROR (Status)) {
+ASSERT(Status != EFI_INVALID_PARAMETER);
+ASSERT(Status != EFI_BUFFER_TOO_SMALL);
+//
+// The following is intended to produce a compilation error on build
+// environments where BUILD_EPOCH can not be set from inline shell.
+// If you are attempting to use this library on such an environment, please
+// contact the edk2 mailing list, so we can try to add support for it.
+//
+ElapsedSeconds = BUILD_EPOCH;
+DEBUG ((
+  DEBUG_INFO,
+  "LibGetTime: %s non volatile variable was not found - Using compilation 
time epoch.\n",
+  mEpochVariableName
+  ));
+  }
+  Counter = GetPerformanceCounter ();
+  ElapsedSeconds += DivU64x64Remainder (Counter, Freq, &Remainder);
+
+  // Get the current time zone information from non-volatile storage
+  Size = sizeof (TimeZone);
+  Status = EfiGetVariable (
+ (CHAR16 *)mTimeZoneVariableName,
+ &gEfiCallerIdGuid,
+ NULL,
+ &Size,
+ (VOID *)&TimeZone
+ );
+
+  if (EFI_ERROR (Status)) {
+ASSERT(Status != EFI_INVALID_PARAMETER);
+ASSER

[edk2] [PATCH 0/1] EmbeddedPkg/Library: Add VirtualRealTimeClockLib

2019-02-04 Thread Pete Batard
This is work, which was requested as part of the Raspberry Pi 3 platform
integration, is designed to remove the need to provide various custom RTC
implementations for platforms that don't have a real RTC.

The library, which is arch-agnostic (through usage of the perf counter) is
designed to emulate as much as it can as of a hardware backed RTC, including
time zones, daylight savings and nanosecond precision.

Note that, since it relies on automatically setting of the epoch value at
compilation-time, it is currently only meant to support gcc based build
environments and relies on UNIX-like shells that provide a 'date' command.
For other environments, the compilation is designed to break in a manner that
will direct the builder to contact the edk2 mailing list, so that we can look
at adding support for their setup.

Regards,

/Pete

Pete Batard (1):
  EmbeddedPkg/Library: Add VirtualRealTimeClockLib

 .../VirtualRealTimeClockLib.c | 400 ++
 .../VirtualRealTimeClockLib.inf   |  43 ++
 2 files changed, 443 insertions(+)
 create mode 100644 
EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
 create mode 100644 
EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf

-- 
2.17.0.windows.1

___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH v4 edk2-platforms 01/23] Silicon/Broadcom/Bcm282x: Add interrupt driver

2019-02-01 Thread Pete Batard

All,

From the feedback below, I'm going to assert that the current consensus 
is to keep Bcm2836.h in IndustryStandard/ and therefore, outside of 
adding an additional description with regards to its purpose, I will 
keep this patch as-is for v5.


If this is not what we want, please let me know.

Regards,

/Pete

On 2019.02.01 08:43, Laszlo Ersek wrote:

On 01/31/19 22:01, Andrew Fish wrote:




On Jan 31, 2019, at 11:57 AM, Leif Lindholm  wrote:

+Andrew, Laszlo, Mike.

On Thu, Jan 31, 2019 at 06:19:48PM +0100, Ard Biesheuvel wrote:

On Thu, 31 Jan 2019 at 16:24, Leif Lindholm  wrote:


On Tue, Jan 29, 2019 at 04:26:33PM +0000, Pete Batard wrote:

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 

Reviewed-by: Ard Biesheuvel 
---
Silicon/Broadcom/Bcm283x/Bcm283x.dec   |  23 ++
Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c   | 367 

Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.inf |  48 +++
Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836.h|  72 


Another generic comment: "IndustryStandard" is something like ACPI,
SMBIOS, PCI, USB, MMC, ... (also including SoC/platform-specific
additions to the same).


Is that your interpretation? Or is this documented somewhere?


Only in asmuch as it is a clearly descriptive name.


I could live with Chipset/, and I'm open to other suggestions, but the
Library vs Protocol vs IndustryStandard distinction is very useful
imo.


It is useful because it is descriptive.
Pretending that an SoC hardware description or a platform description
header is an "Industry Standard" is disingenuous.


I would be more comfortable with SoC-specific and Platform-specific
include files living directly in Include/.


No, don't drop headers in Include/ please. The namespacing is one of
the things EDK2 actually gets right (assuming you define the paths
correctly in the package .dec file), and I'd hate to start dumping
headers at the root level because we cannot make up our minds what to
call the enclosing folder.


Mike, Andrew - what is your take on this?
Is there a formal definition of not only what goes in
IndustryStandard, but where chipset and platform headers should live
in the namespace?



Leif,

I kind of think IndustryStandard as things that have a public spec


I think the same. I think any device / interface headers can go under
Include/IndustryStandard as long as the interface was explicitly
designed for external consumption, and is promised to be stable.

I realize some packages have Include/Register too... I find that a bit
redundant.

Thanks
Laszlo



___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH v4 edk2-platforms 20/23] Platform/Raspberry/Pi3: Add platform readme

2019-01-31 Thread Pete Batard

On 2019.01.31 14:44, Ard Biesheuvel wrote:

On Thu, 31 Jan 2019 at 15:36, Ard Biesheuvel  wrote:


On Thu, 31 Jan 2019 at 15:13, Leif Lindholm  wrote:


On Thu, Jan 31, 2019 at 12:30:22PM +, Pete Batard wrote:

Hi Leif. Thanks for reviewing this patchset.

On 2019.01.30 21:50, Leif Lindholm wrote:

Hi Pete,

I will only have minor comments on this set, but I'll start with this
documentation.

On Tue, Jan 29, 2019 at 04:26:52PM +0000, Pete Batard wrote:

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
   Platform/Raspberry/Pi3/Readme.md | 259 
   Readme.md|   3 +
   2 files changed, 262 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Readme.md b/Platform/Raspberry/Pi3/Readme.md
new file mode 100644
index ..7fb59ccdc321
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Readme.md
@@ -0,0 +1,259 @@
+Raspberry Pi 3 EDK2 Platform Support
+
+
+# Summary
+
+This is a port of 64-bit Tiano Core UEFI firmware for the Raspberry Pi 3/3B+ 
platforms,
+based on [Ard Bisheuvel's 
64-bit](http://www.workofard.com/2017/02/uefi-on-the-pi/)
+and [Microsoft's 
32-bit](https://github.com/ms-iot/RPi-UEFI/tree/ms-iot/Pi3BoardPkg)
+implementations, as maintained by [Andrei 
Warkentin](https://github.com/andreiw/RaspberryPiPkg).
+
+This is meant as a generally useful 64-bit ATF + UEFI implementation for the 
Raspberry
+Pi 3/3B+ which should be good enough for most kind of UEFI development, as 
well as for
+running consummer Operating Systems in such as Linux or Windows.
+
+Raspberry Pi is a trademark of the [Raspberry Pi 
Foundation](http://www.raspberrypi.org).
+
+# Status
+
+This firmware, that has been validated to compile against the current
+[edk2](https://github.com/tianocore/edk2)/[edk2-platforms](https://github.com/tianocore/edk2-platforms),
+should be able to boot Linux (SUSE, Ubuntu), NetBSD, FreeBSD as well as 
Windows 10 ARM64
+(full GUI version).
+
+It also provides support for ATF ([Arm Trusted 
Platform](https://github.com/ARM-software/arm-trusted-firmware)).
+
+HDMI and the mini-UART serial port can be used for output devices, with 
mirrored output.
+USB keyboards and the mini-UART serial port can be used as input.
+
+The boot order is currently hardcoded, first to the USB ports and then to the 
uSD card.
+If there no bootable media media is found, the UEFI Shell is launched.
+Esc enters platform setup. F1 boots the UEFI Shell.
+
+# Building
+
+(These instructions were validated against the latest edk2 / edk2-platforms /
+edk2-non-osi as of 2019.01.27, on a Debian 9.6 x64 system).
+
+You may need to install the relevant compilation tools. Especially you should 
have the
+ACPI Source Language (ASL) compiler, `nasm` as well as a native compiler 
installed.


nasm? The x86 assembler?


I'll remove that.


+On a Debian system, you can get these prerequisites installed with:
+```
+sudo apt-get install build-essential acpica-tools nasm uuid-dev
+```
+
+**IMPORTANT:** We recommend the use of the Linaro GCC for compilation instead 
of
+your system's native ARM64 GCC cross compiler.


This sounds like something written in the days of GCC 4.8. I doubt it
has any relevance today.


It very much had until circa one month ago, as we observed early Synchronous
Exceptions when trying to use the native Debian ARM64 compiler, which we did
not observe with Linaro's toolchain. We even had trouble (similar issue)
with recent Linaro toolchains at some stage, which is why, until v3, we
recommended an older version, but recent tests showed that the latest Linaro
GCC (2019.02) appeared to be okay, which is why I removed the previous
requirement to use exclusively Linaro's 2017.10 GCC 5.5.


Urgh. But that actually makes the above statement even more
misleading. What you have isn't an issue with non-Linaro toolchains,
you have an unidentified toolchain issue that you've triggered more
frequently



Could you please check whether the broken toolchain in question has
the workaround for Cortex-A53 erratum 843419 enabled? (gcc -v will
tell you)


It is.

On that subject, I have now ran a new series of tests, with the default 
AARCH64 Debian 9.7 compiler (gcc 6.3.0 20170516), and so far I have not 
been able to observe the Synchronous Exceptions that had us switch to 
the Linaro compiler.


It needs to be noted however that, since we ran into those, we did 
switch to using different ATF binaries, different VideoCore firmware, 
and removed some drivers (such as an HyperVisor) from the firmware. So 
there are quite a few elements that could have had an impact on the 
earlier issue.


I will continue to test with the default Debian compiler for now, and, 
if I don't see a resurgence of the issue, I'll remove the note about 
preferring a Linaro toolchain.



Also, given that Leif doesn't want the full hand-holding compilation 
instructions and that I have

Re: [edk2] [PATCH v4 edk2-platforms 06/23] Platform/Raspberry/Pi3: Add RTC library

2019-01-31 Thread Pete Batard

On 2019.01.30 22:22, Leif Lindholm wrote:

First of all - this is something I would like to see contributed
directly to edk2 EmbeddedPkg. It's something me and Ard have discussed
adding at some point, but never getting around to.

I would also like to look into whether we could replace
PcdBootEpochSeconds with a -DBUILD_EPOCH=`date +%s` addition to the
CFLAGS in the .inf.


Okay, I'll look into both of these items.



Regards,

Leif

On Tue, Jan 29, 2019 at 04:26:38PM +0000, Pete Batard wrote:

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  
Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
   | 221 
  
Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
 |  43 
  2 files changed, 264 insertions(+)

diff --git 
a/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
 
b/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
new file mode 100644
index ..dea621ff03ef
--- /dev/null
+++ 
b/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
@@ -0,0 +1,221 @@
+/** @file
+ *
+ *  Implement dummy EFI RealTimeClock runtime services.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/**
+   Returns the current time and date information, and the time-keeping 
capabilities
+   of the virtual RTC.
+
+   For simplicity, this LibGetTime does not report Years/Months, instead it 
will only report current
+   Day, Hours, Minutes and Seconds starting from the begining of CPU up-time. 
Otherwise, a more
+   complex logic will be required to account for leap years and days/month 
differences.
+
+   @param  Time  A pointer to storage to receive a snapshot of 
the current time.
+   @param  Capabilities  An optional pointer to a buffer to receive 
the real time clock
+   device's capabilities.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER Time is NULL.
+   @retval EFI_DEVICE_ERROR  The time could not be retrieved due to 
hardware error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibGetTime (
+  OUT EFI_TIME   *Time,
+  OUT EFI_TIME_CAPABILITIES  *Capabilities
+  )
+{
+  UINTN DataSize;
+  UINT64 Counter;
+  EFI_STATUS Status;
+  UINTN ElapsedSeconds;
+  UINT32 Remainder;
+  UINT32 Freq = ArmGenericTimerGetTimerFreq ();
+
+  if (Time == NULL) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Depend on ARM generic timer to report date/time relative to the
+  // start of CPU timer counting where date and time will always
+  // be relative to the date/time 1/1/1900 00H:00M:00S
+  //
+
+  ASSERT (Freq != 0);
+  if (Freq == 0) {
+return EFI_DEVICE_ERROR;
+  }
+
+  if (Capabilities) {
+Capabilities->Accuracy = 0;
+Capabilities->Resolution = Freq;
+Capabilities->SetsToZero = FALSE;
+  }
+
+  DataSize = sizeof (UINTN);
+  ElapsedSeconds = 0;
+  Status = EfiGetVariable (L"RtcEpochSeconds",
+ &gEfiCallerIdGuid,
+ NULL,
+ &DataSize,
+ &ElapsedSeconds);
+  if (EFI_ERROR (Status)) {
+ElapsedSeconds = PcdGet64 (PcdBootEpochSeconds);
+  }
+  Counter = GetPerformanceCounter ();
+  ElapsedSeconds += DivU64x32Remainder (Counter, Freq, &Remainder);
+  EpochToEfiTime (ElapsedSeconds, Time);
+
+  //
+  // Frequency < 0x1, so Remainder < 0x1, then (Remainder * 
1,000,000,000)
+  // will not overflow 64-bit.
+  //
+  Time->Nanosecond = DivU64x32 (MultU64x64 ((UINT64)Remainder, 10U), 
Freq);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+   Sets the current local time and date information.
+
+   @param  Time  A pointer to the current time.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER A time field is out of range.
+   @retval EFI_DEVICE_ERROR  The time could not be set due due to hardware 
error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibSetTime (
+  IN EFI_TIME  *Time
+  )
+{
+  UINTN Epoch;
+
+  if (!IsTimeValid (Time)) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  Epoch = EfiTimeToEpoch (Time);
+  return EfiSetVariable (L"RtcEpochSeconds", &gEfiCallerIdGuid,
+ 

Re: [edk2] [PATCH v4 edk2-platforms 00/23] Platform/Raspberry: Add Raspberry Pi 3 support

2019-01-31 Thread Pete Batard

On 2019.01.30 21:59, Leif Lindholm wrote:

Hi Pete,

I have two annoying pieces of feedback that apply to the whole set.

Firstly, I would really appreciate if we could have some sort of
commit messages rather than just subject lines.


I can work on that.


So, for ACPI, mention the provenance and limitations.
For SMBIOS, version supported and any relevant omissions or surprising
inclusions. And so on.

Secondly, we try to follow a //... pattern.
Could you possibly find it in your heart to do a global move and
search-and-replace of Platform/Raspberry to Platform/RaspberryPi?


Yes. I am realizing now that I thought it was called the "Raspberry 
Foundation", with the expectation that maybe in the future they might 
release a new platform called "Raspberry Jam" or something, hence my use 
of "Raspberry" for the organization. But it's correct denomination is 
really the "Raspberry Pi Foundation", so your remark does make sense.


And since it won't matter much to go one step further, I'll also apply 
the follow up remark to have:


Platform/RaspberryPi/RPi3/RPi3.dsc.


Best Regards,

Leif

On Tue, Jan 29, 2019 at 04:26:32PM +, Pete Batard wrote:

Changes applied to v4:

* Silicon/Broadcom/Include has been moved to Silicon/Broadcom/Bcm283x/Include.
   The [Packages] and [Includes] directives were also updated accordingly.
* Move the GpioLib function declarations into their own separate header.
* Add NOOPT to BUILD_TARGETS.
* Remove the no longer needed '-mcmodel=small' workaround from AcpiTables.

Changes not applied to v4:

* Ensure that all the ACPI tables _CID names, and the rest of the tables, are
   ACPI specs compliant, since we are constrained with regards to their usage
   for Microsoft Windows.


Preamble:

Because of its price point, ease of use and availability, the Raspberry Pi is
undeniably one of the most successful ARM platform in existence today. Its
widespread adoption therefore makes it a perfect fit as an EDK2 platform.

However, up until now, the Raspberry Pi hasn't been supported as a bona fide
platform in our repository. This series of patches remedies that by introducing
the Raspberry Pi 3 Model B and Model B+ as a viable EDK2 platform.

Notes regarding non-OSI content:

* Even though the ARM Trusted Firmware binary blobs are subject to a
   BSD-3-Clause licence, which may be compatible with the EDK2 one, we chose
   to follow the lead of other platforms that provide ATF binaries in non OSI.
   Ultimately, once there is a new dot release of ATF, we plan to remove these
   binaries and point to a dot release build configuartion.
* The Device Tree binaries (and source descriptors) are subject to a GPLv2
   license, as per the ones published by the Raspberry Pi Foundation.
* The Logo source code is under an EDK2 license, but the logo itself, which
   we obtained authorisation to use from the Raspberry Pi Foundation itself
   after detailing our planned usage, is subject to the trademark licensing
   terms put forward by the Foundation.

Additional Notes:

* Detailed instructions on how to build and test the platform firmware are
   included in the Readme.md found at the root of the platform.
* As detailed in the Readme, the resulting platform firmware has been
   successfully used to install and run Linux OSes, such as Ubuntu 18.10, as
   well as Windows 10 1809 (*full* UI version, not IoT).

Regards,

/Pete

Pete Batard (23):
   Silicon/Broadcom/Bcm282x: Add interrupt driver
   Silicon/Broadcom/Bcm283x: Add GpioLib
   Platform/Raspberry/Pi3: Add ACPI tables
   Platform/Raspberry/Pi3: Add reset and memory init libraries
   Platform/Raspberry/Pi3: Add platform library
   Platform/Raspberry/Pi3: Add RTC library
   Platform/Raspberry/Pi3: Add firmware driver
   Platform/Raspberry/Pi3: Add platform config driver
   Platform/Raspberry/Pi3: Add SMBIOS driver
   Platform/Raspberry/Pi3: Add display driver
   Platform/Raspberry/Pi3: Add console driver
   Platform/Raspberry/Pi3: Add NV storage driver
   Platform/Raspberry/Pi3: Add Device Tree driver
   Platform/Raspberry/Pi3: Add base MMC driver
   Platform/Raspberry/Pi3: Add Arasan MMC driver
   Platform/Raspberry/Pi3: Platform/Raspberry/Pi3: Add SD Host driver
   Platform/Raspberry/Pi3: Add platform boot manager and helper libraries
   Platform/Raspberry/Pi3: Add USB host driver
   Platform/Raspberry/Pi3: Add platform
   Platform/Raspberry/Pi3: Add platform readme
   Platform/Raspberry/Pi3 *NON-OSI*: Add ATF binaries
   Platform/Raspberry/Pi3 *NON-OSI*: Add Device Tree binaries
   Platform/Raspberry/Pi3 *NON-OSI*: Add logo driver

  .../Raspberry/Pi3/AcpiTables/AcpiTables.h |   82 +
  .../Raspberry/Pi3/AcpiTables/AcpiTables.inf   |   46 +
  Platform/Raspberry/Pi3/AcpiTables/Csrt.aslc   |  332 +++
  Platform/Raspberry/Pi3/AcpiTables/Dbg2.aslc   |   34 +
  Platform/Raspberry/Pi3/AcpiTables/Dsdt.asl|  511 +
  Platform/Raspberry/Pi3/AcpiTables/Fadt.aslc   |   52

Re: [edk2] [PATCH v4 edk2-platforms 20/23] Platform/Raspberry/Pi3: Add platform readme

2019-01-31 Thread Pete Batard

Hi Leif. Thanks for reviewing this patchset.

On 2019.01.30 21:50, Leif Lindholm wrote:

Hi Pete,

I will only have minor comments on this set, but I'll start with this
documentation.

On Tue, Jan 29, 2019 at 04:26:52PM +0000, Pete Batard wrote:

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/Raspberry/Pi3/Readme.md | 259 
  Readme.md|   3 +
  2 files changed, 262 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Readme.md b/Platform/Raspberry/Pi3/Readme.md
new file mode 100644
index ..7fb59ccdc321
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Readme.md
@@ -0,0 +1,259 @@
+Raspberry Pi 3 EDK2 Platform Support
+
+
+# Summary
+
+This is a port of 64-bit Tiano Core UEFI firmware for the Raspberry Pi 3/3B+ 
platforms,
+based on [Ard Bisheuvel's 
64-bit](http://www.workofard.com/2017/02/uefi-on-the-pi/)
+and [Microsoft's 
32-bit](https://github.com/ms-iot/RPi-UEFI/tree/ms-iot/Pi3BoardPkg)
+implementations, as maintained by [Andrei 
Warkentin](https://github.com/andreiw/RaspberryPiPkg).
+
+This is meant as a generally useful 64-bit ATF + UEFI implementation for the 
Raspberry
+Pi 3/3B+ which should be good enough for most kind of UEFI development, as 
well as for
+running consummer Operating Systems in such as Linux or Windows.
+
+Raspberry Pi is a trademark of the [Raspberry Pi 
Foundation](http://www.raspberrypi.org).
+
+# Status
+
+This firmware, that has been validated to compile against the current
+[edk2](https://github.com/tianocore/edk2)/[edk2-platforms](https://github.com/tianocore/edk2-platforms),
+should be able to boot Linux (SUSE, Ubuntu), NetBSD, FreeBSD as well as 
Windows 10 ARM64
+(full GUI version).
+
+It also provides support for ATF ([Arm Trusted 
Platform](https://github.com/ARM-software/arm-trusted-firmware)).
+
+HDMI and the mini-UART serial port can be used for output devices, with 
mirrored output.
+USB keyboards and the mini-UART serial port can be used as input.
+
+The boot order is currently hardcoded, first to the USB ports and then to the 
uSD card.
+If there no bootable media media is found, the UEFI Shell is launched.
+Esc enters platform setup. F1 boots the UEFI Shell.
+
+# Building
+
+(These instructions were validated against the latest edk2 / edk2-platforms /
+edk2-non-osi as of 2019.01.27, on a Debian 9.6 x64 system).
+
+You may need to install the relevant compilation tools. Especially you should 
have the
+ACPI Source Language (ASL) compiler, `nasm` as well as a native compiler 
installed.


nasm? The x86 assembler?


I'll remove that.


+On a Debian system, you can get these prerequisites installed with:
+```
+sudo apt-get install build-essential acpica-tools nasm uuid-dev
+```
+
+**IMPORTANT:** We recommend the use of the Linaro GCC for compilation instead 
of
+your system's native ARM64 GCC cross compiler.


This sounds like something written in the days of GCC 4.8. I doubt it
has any relevance today.


It very much had until circa one month ago, as we observed early 
Synchronous Exceptions when trying to use the native Debian ARM64 
compiler, which we did not observe with Linaro's toolchain. We even had 
trouble (similar issue) with recent Linaro toolchains at some stage, 
which is why, until v3, we recommended an older version, but recent 
tests showed that the latest Linaro GCC (2019.02) appeared to be okay, 
which is why I removed the previous requirement to use exclusively 
Linaro's 2017.10 GCC 5.5.


Besides, I think it's preferable when a project highlights precisely how 
they build their own binaries, and with which toolchain, so that, if 
anybody experiences an issue with their own build, they can compare 
their setup with the maintainer's "official" one.



Also, and this applies both above and below: I am trying very hard to
get rid of (mostly unnecessary) platform-specific build instructions.
The top-level Readme.md in this repository contains basic build
instructions. I would much prefer if you can refer to that instead and
drop everything after the # Building header above...


Okay. This is a bit at odds with the goal I'm trying to achieve here, 
which is to save time and bewilderment from developers who might be 
trying to build this specific platform and encounter an issue where 
they'll want to eliminate the possibility that their setup/configuration 
is the problem.


I've ran in too many of "works on my machine" not top want to also 
provide "here is exactly how the official developer's machine was set up 
when they ran their built" from the get go, to try to alleviate the 
usual headaches of trying to solve environmental issues... Furthermore, 
given the popularity of the Raspberry Pi platform, my guess is that 
we're going to get quite a few people who aren't that familiar with the 
EDK2, or even building

Re: [edk2] [PATCH v4 edk2-platforms 00/23] Platform/Raspberry: Add Raspberry Pi 3 support

2019-01-29 Thread Pete Batard

On 2019.01.29 17:40, Ard Biesheuvel wrote:

I managed to build and run a RELEASE image, and I must say, I'm
impressed. It works really well, and looks really slick too. Thanks a
lot for taking the time to upstream this, it makes the RPI3 a lot more
usable for people that want to run a generic distro.


I'm very happy to hear that.

Indeed, our hope is that, once the firmware gets "officialized" into 
edk2-platforms, mainline Linux distros will switch to using its 
facilities and make the whole Linux boot process a friendlier/more 
familiar experience for Pi3 users.


Of course, you have to thank Andrei Warkentin for this nice outcome, 
since he put a lot of effort into it, as well as all the people who 
contributed to the firmware (including Linaro & Microsoft).



Tested-by: Ard Biesheuvel 


Many thanks for this.

/Pete
___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


[edk2] [PATCH v4 edk2-platforms 22/23] Platform/Raspberry/Pi3 *NON-OSI*: Add Device Tree binaries

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/DeviceTree/License.txt  |  340 ++
 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b-plus.dtb |  Bin 0 -> 25617 
bytes
 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b-plus.dts | 1263 

 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b.dtb  |  Bin 0 -> 25354 
bytes
 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b.dts  | 1259 
+++
 5 files changed, 2862 insertions(+)

diff --git a/Platform/Raspberry/Pi3/DeviceTree/License.txt 
b/Platform/Raspberry/Pi3/DeviceTree/License.txt
new file mode 100644
index ..1603937dad82
--- /dev/null
+++ b/Platform/Raspberry/Pi3/DeviceTree/License.txt
@@ -0,0 +1,340 @@
+GNU GENERAL PUBLIC LICENSE
+   Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy a

[edk2] [PATCH v4 edk2-platforms 21/23] Platform/Raspberry/Pi3 *NON-OSI*: Add ATF binaries

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/TrustedFirmware/License.txt |  26 
 Platform/Raspberry/Pi3/TrustedFirmware/README.md   |  42 
 Platform/Raspberry/Pi3/TrustedFirmware/bl1.bin | Bin 0 -> 18801 bytes
 Platform/Raspberry/Pi3/TrustedFirmware/fip.bin | Bin 0 -> 41714 bytes
 4 files changed, 68 insertions(+)

diff --git a/Platform/Raspberry/Pi3/TrustedFirmware/License.txt 
b/Platform/Raspberry/Pi3/TrustedFirmware/License.txt
new file mode 100644
index ..b98dc643227e
--- /dev/null
+++ b/Platform/Raspberry/Pi3/TrustedFirmware/License.txt
@@ -0,0 +1,26 @@
+Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without 
modification,
+are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice, 
this
+  list of conditions and the following disclaimer in the documentation and/or
+  other materials provided with the distribution.
+
+* Neither the name of ARM nor the names of its contributors may be used to
+  endorse or promote products derived from this software without specific prior
+  written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Platform/Raspberry/Pi3/TrustedFirmware/README.md 
b/Platform/Raspberry/Pi3/TrustedFirmware/README.md
new file mode 100644
index ..862933f6f2ba
--- /dev/null
+++ b/Platform/Raspberry/Pi3/TrustedFirmware/README.md
@@ -0,0 +1,42 @@
+ARM Trusted Firmware for Raspberry Pi 3
+===
+
+The `bl1` and `fip` ATF binaries, found in this directory, were built from
+the [latest ATF](https://github.com/ARM-software/arm-trusted-firmware)
+(commit c3859557) using Linaro's GCC 5.5 compiler with:
+
+```
+export 
CROSS_COMPILE=/usr/src/gcc-linaro-5.5.0-2017.10-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
+make PLAT=rpi3 PRELOADED_BL33_BASE=0x3 RPI3_PRELOADED_DTB_BASE=0x1 
SUPPORT_VFP=1 RPI3_USE_UEFI_MAP=1 fip all
+```
+
+This results in the following memory mapping:
+
+```
+0x +-+
+   |   ROM   | BL1
+0x0001 +-+
+   |   DTB   | (Loaded by the VideoCore)
+0x0002 +-+
+   |   FIP   |
+0x0003 +-+
+   | |
+   |  UEFI PAYLOAD   |
+   | |
+0x0020 +-+
+   |   Secure SRAM   | BL2, BL31
+0x0030 +-+
+   |   Secure DRAM   | BL32 (Secure payload)
+0x0040 +-+
+   | |
+   | |
+   | Non-secure DRAM | BL33
+   | |
+   | |
+0x0100 +-+
+   | |
+   |   ...   |
+   | |
+0x3F00 +-+
+   |   I/O   |
+```
diff --git a/Platform/Raspberry/Pi3/TrustedFirmware/bl1.bin 
b/Platform/Raspberry/Pi3/TrustedFirmware/bl1.bin
new file mode 100644
index 
..e25138828d0a4ddb24772abb1a60eefc334666b5
GIT binary patch
literal 18801
zcmeHud2|%#mG7;pmedFVtpy|tJ-S*z4DF582<)+{TLO%YF^IMyw%@2*ivhC;Enpe2
zRTADp&Ya2NoOkF$@l}1x-M+hhw@Q@U820D96C>N0$f-gpvNVO-6r%k^_LFjhV53f>
zzYYB^p(#wZhayBm(40?m)v%ZRF-jkU{?@RJdpUDVq_0@Qvl>>=KM8BSMAN6D9c2X^
zv}!gTC9-IZXdThl_aE5oz6A)Ol0}J=O^M4yanPmrUEBE6z%_}qi$f`XQKFzu
zlVZ_?G$rnX41{DgYzORyWMj?6p-TMP#*g+gylWdj3`8X%sIOlv1lmB)R$NV@Mf2q+
zY4d>}JWrcSYDNxGR%4SA-?^DI?>kETG0>;<^-cF@D)C*r!0X4PiJ}^Q9sQugsR%T^
zW2zF8L8m(ZKusahN`})&mx1t%~~5>0PQR^krEJ6wr3iAo6aZSmWc_!Ag6
z`56X&xUWmf4DJH$r=?NBrxUaj_-e#D|7a!d7m4LPR23=2^S_NbBGxi5Ce4?hcK6f$
z3TXVu>I#)XZeBsnU^%~vzTYcJS`*qLzY-q@C2Lj%?d)EP7gLhfKy+cZh2moYa~$X{
zx3%XUwhAHXdf#-#Z|vK6?76XLKQ{Io`^9@z&g

[edk2] [PATCH v4 edk2-platforms 20/23] Platform/Raspberry/Pi3: Add platform readme

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Readme.md | 259 
 Readme.md|   3 +
 2 files changed, 262 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Readme.md b/Platform/Raspberry/Pi3/Readme.md
new file mode 100644
index ..7fb59ccdc321
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Readme.md
@@ -0,0 +1,259 @@
+Raspberry Pi 3 EDK2 Platform Support
+
+
+# Summary
+
+This is a port of 64-bit Tiano Core UEFI firmware for the Raspberry Pi 3/3B+ 
platforms,
+based on [Ard Bisheuvel's 
64-bit](http://www.workofard.com/2017/02/uefi-on-the-pi/)
+and [Microsoft's 
32-bit](https://github.com/ms-iot/RPi-UEFI/tree/ms-iot/Pi3BoardPkg)
+implementations, as maintained by [Andrei 
Warkentin](https://github.com/andreiw/RaspberryPiPkg).
+
+This is meant as a generally useful 64-bit ATF + UEFI implementation for the 
Raspberry
+Pi 3/3B+ which should be good enough for most kind of UEFI development, as 
well as for
+running consummer Operating Systems in such as Linux or Windows.
+
+Raspberry Pi is a trademark of the [Raspberry Pi 
Foundation](http://www.raspberrypi.org).
+
+# Status
+
+This firmware, that has been validated to compile against the current
+[edk2](https://github.com/tianocore/edk2)/[edk2-platforms](https://github.com/tianocore/edk2-platforms),
+should be able to boot Linux (SUSE, Ubuntu), NetBSD, FreeBSD as well as 
Windows 10 ARM64
+(full GUI version).
+
+It also provides support for ATF ([Arm Trusted 
Platform](https://github.com/ARM-software/arm-trusted-firmware)).
+
+HDMI and the mini-UART serial port can be used for output devices, with 
mirrored output.
+USB keyboards and the mini-UART serial port can be used as input.
+
+The boot order is currently hardcoded, first to the USB ports and then to the 
uSD card.
+If there no bootable media media is found, the UEFI Shell is launched.
+Esc enters platform setup. F1 boots the UEFI Shell.
+
+# Building
+
+(These instructions were validated against the latest edk2 / edk2-platforms /
+edk2-non-osi as of 2019.01.27, on a Debian 9.6 x64 system).
+
+You may need to install the relevant compilation tools. Especially you should 
have the
+ACPI Source Language (ASL) compiler, `nasm` as well as a native compiler 
installed.
+On a Debian system, you can get these prerequisites installed with:
+```
+sudo apt-get install build-essential acpica-tools nasm uuid-dev
+```
+
+**IMPORTANT:** We recommend the use of the Linaro GCC for compilation instead 
of
+your system's native ARM64 GCC cross compiler.
+
+You can then build the firmware as follows:
+
+* Standalone instructions
+
+```
+mkdir ~/workspace
+cd ~/workspace
+git clone https://github.com/tianocore/edk2.git
+# The following is only needed once, after you cloned edk2
+make -C edk2/BaseTools
+git clone https://github.com/tianocore/edk2-platforms.git
+git clone https://github.com/tianocore/edk2-non-osi.git
+wget 
https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/aarch64-linux-gnu/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz
+tar -xJvf gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz
+# If you have multiple AARCH64 toolchains, make sure the above one comes first 
in your path
+export PATH=$PWD/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu/bin:$PATH
+export GCC5_AARCH64_PREFIX=aarch64-linux-gnu-
+export WORKSPACE=$PWD
+export 
PACKAGES_PATH=$WORKSPACE/edk2:$WORKSPACE/edk2-platforms:$WORKSPACE/edk2-non-osi
+. edk2/edksetup.sh
+build -a AARCH64 -t GCC5 -p edk2-platforms/Platform/Raspberry/Pi3/RPi3.dsc 
-DBUILD_EPOCH=`date +%s` -b RELEASE
+```
+
+# Booting the firmware
+
+1. Format a uSD card as FAT32
+2. Copy the generated `RPI_EFI.fd` firmware onto the partition
+3. Download and copy the following files from 
https://github.com/raspberrypi/firmware/tree/master/boot
+  - `bootcode.bin`
+  - `fixup.dat`
+  - `start.elf`
+4. Create a `config.txt` with the following content:
+  ```
+  arm_control=0x200
+  enable_uart=1
+  armstub=RPI_EFI.fd
+  disable_commandline_tags=1
+  ```
+5. Insert the uSD card and power up the Pi.
+
+Note that if you have a model 3+ or a model 3 where you enabled USB boot 
through OTP
+(see 
[here](https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/msd.md))
+you may also be able to boot from a FAT32 USB driver rather than uSD.
+
+# Notes
+
+## ARM Trusted Firmware (ATF)
+
+The ATF binaries being used were compiled from the ATF mainline.
+
+For more details on the ATF compilation, see the [README](./Binary/README.md) 
in the `Binary/` directory.
+
+## Custom Device Tree
+
+The default Device Tree included in the firmware is the one for a Raspberry Pi 
3 Model B (not B+).
+If you want to use a different Device Tree, to boot a Pi 3 Model B+ for 
instance (for which a
+DTB is also provided under `DeviceTree/`), you should copy the relevant `.dtb` 
into the root of
+the

[edk2] [PATCH v4 edk2-platforms 19/23] Platform/Raspberry/Pi3: Add platform

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/RPi3.dec |  59 ++
 Platform/Raspberry/Pi3/RPi3.dsc | 637 
 Platform/Raspberry/Pi3/RPi3.fdf | 449 ++
 3 files changed, 1145 insertions(+)

diff --git a/Platform/Raspberry/Pi3/RPi3.dec b/Platform/Raspberry/Pi3/RPi3.dec
new file mode 100644
index ..a4c12e332676
--- /dev/null
+++ b/Platform/Raspberry/Pi3/RPi3.dec
@@ -0,0 +1,59 @@
+## @file
+#
+#  Copyright (c) 2016, Linaro, Ltd. All rights reserved.
+#  Copyright (c) 2017-2018, Andrei Warkentin 
+#
+#  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.
+#
+##
+
+[Defines]
+  DEC_SPECIFICATION  = 0x0001001A
+  PACKAGE_NAME   = RPi3Pkg
+  PACKAGE_GUID   = DFA0CA8B-F3AC-4607-96AC-46FA04B84DCC
+  PACKAGE_VERSION= 1.0
+
+[Includes]
+  Include
+
+[Protocols]
+  gRaspberryPiFirmwareProtocolGuid = { 0x0ACA9535, 0x7AD0, 0x4286, { 0xB0, 
0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiConfigAppliedProtocolGuid = { 0x0ACA, 0x7AD0, 0x4286, { 
0xB0, 0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiMmcHostProtocolGuid = { 0x3e591c00, 0x9e4a, 0x11df, {0x92, 0x44, 
0x00, 0x02, 0xA5, 0xF5, 0xF5, 0x1B } }
+  gExtendedTextOutputProtocolGuid = { 0x387477ff, 0xffc7, 0xffd2, {0x8e, 0x39, 
0x0, 0xff, 0xc9, 0x69, 0x72, 0x3b } }
+
+[Guids]
+  gRaspberryPiTokenSpaceGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+  gRaspberryPiFdtFileGuid = {0xDF5DA223, 0x1D27, 0x47C3, { 0x8D, 0x1B, 0x9A, 
0x41, 0xB5, 0x5A, 0x18, 0xBC}}
+  gRaspberryPiEventResetGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB4, 0xB4, 0xE4, 0xD4, 0xB4}}
+  gConfigDxeFormSetGuid = {0xCD7CC258, 0x31DB, 0x22E6, {0x9F, 0x22, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+
+[PcdsFixedAtBuild.common]
+  gRaspberryPiTokenSpaceGuid.PcdFdtBaseAddress|0x1|UINT32|0x0001
+  gRaspberryPiTokenSpaceGuid.PcdFirmwareBlockSize|0x0|UINT32|0x0002
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogBase|0x0|UINT32|0x0003
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogSize|0x0|UINT32|0x0004
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageVariableBase|0x0|UINT32|0x0005
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwSpareBase|0x0|UINT32|0x0006
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwWorkingBase|0x0|UINT32|0x0007
+  gRaspberryPiTokenSpaceGuid.PcdBootEpochSeconds|0x0|UINT64|0x0008
+
+[PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx]
+  gRaspberryPiTokenSpaceGuid.PcdCpuClock|0|UINT32|0x000d
+  gRaspberryPiTokenSpaceGuid.PcdSdIsArasan|0|UINT32|0x000e
+  gRaspberryPiTokenSpaceGuid.PcdMmcForce1Bit|0|UINT32|0x000f
+  gRaspberryPiTokenSpaceGuid.PcdMmcForceDefaultSpeed|0|UINT32|0x0010
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdDefaultSpeedMHz|0|UINT32|0x0011
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdHighSpeedMHz|0|UINT32|0x0012
+  gRaspberryPiTokenSpaceGuid.PcdMmcDisableMulti|0|UINT32|0x0013
+  gRaspberryPiTokenSpaceGuid.PcdDebugEnableJTAG|0|UINT32|0x0014
+  gRaspberryPiTokenSpaceGuid.PcdDebugShowUEFIExit|0|UINT32|0x0015
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableVModes|0|UINT32|0x0017
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableSShot|0|UINT32|0x0018
diff --git a/Platform/Raspberry/Pi3/RPi3.dsc b/Platform/Raspberry/Pi3/RPi3.dsc
new file mode 100644
index ..4d43363e433d
--- /dev/null
+++ b/Platform/Raspberry/Pi3/RPi3.dsc
@@ -0,0 +1,637 @@
+# @file
+#
+#  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
+#  Copyright (c) 2014, Linaro Limited. All rights reserved.
+#  Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
+#  Copyright (c) 2017 - 2018, Andrei Warkentin 
+#
+#  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.
+#
+##
+
+
+#
+# Defines Section - statements that will be processed to create a Makefile.
+#
+
+[Defines]
+  PLATFORM_NAME  = RPi3
+  PLATFORM_GUID  = 5d30c4fc-93cf-40

[edk2] [PATCH v4 edk2-platforms 18/23] Platform/Raspberry/Pi3: Add USB host driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c  |  226 +++
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DriverBinding.c  |  275 
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.c   | 1637 

 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.h   |  165 ++
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.inf |   53 +
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwcHw.h  |  791 ++
 Platform/Raspberry/Pi3/Include/Protocol/DwUsb.h  |   53 +
 7 files changed, 3200 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c
new file mode 100644
index ..0f4db4e21204
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c
@@ -0,0 +1,226 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrey Warkentin 
+ *
+ *
+ *  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 "DwUsbHostDxe.h"
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  );
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL *This,
+  IN  EFI_HANDLE  ControllerHandle,
+  IN  EFI_HANDLE  ChildHandle,
+  IN  CHAR8   *Language,
+  OUT CHAR16  **ControllerName
+  );
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
+  ComponentNameGetDriverName,
+  ComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)ComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)ComponentNameGetControllerName,
+  "en"
+};
+
+
+STATIC EFI_UNICODE_STRING_TABLE mDriverName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi USB Host Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mDeviceName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi USB Host"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  E

[edk2] [PATCH v4 edk2-platforms 17/23] Platform/Raspberry/Pi3: Add platform boot manager and helper libraries

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c 
  | 793 
 Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.h 
  |  60 ++
 
Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
 |  90 +++
 Platform/Raspberry/Pi3/Library/PlatformUiAppLib/PlatformUiAppLib.c 
  | 120 +++
 Platform/Raspberry/Pi3/Library/PlatformUiAppLib/PlatformUiAppLib.inf   
  |  34 +
 5 files changed, 1097 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c 
b/Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c
new file mode 100644
index ..9bbe0db64950
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c
@@ -0,0 +1,793 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Pete Batard 
+ *  Copyright (c) 2017-2018, Andrei Warkentin 
+ *  Copyright (c) 2016, Linaro Ltd. All rights reserved.
+ *  Copyright (c) 2015-2016, Red Hat, Inc.
+ *  Copyright (c) 2014, ARM Ltd. All rights reserved.
+ *  Copyright (c) 2004-2016, Intel Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "PlatformBm.h"
+
+#define BOOT_PROMPT L"ESC (setup), F1 (shell), ENTER (boot)"
+
+#define DP_NODE_LEN(Type) { (UINT8)sizeof (Type), (UINT8)(sizeof (Type) >> 8) }
+
+#pragma pack (1)
+typedef struct {
+  VENDOR_DEVICE_PATH SerialDxe;
+  UART_DEVICE_PATH   Uart;
+  VENDOR_DEFINED_DEVICE_PATH TermType;
+  EFI_DEVICE_PATH_PROTOCOL   End;
+} PLATFORM_SERIAL_CONSOLE;
+#pragma pack ()
+
+typedef struct {
+  VENDOR_DEVICE_PATH Custom;
+  USB_DEVICE_PATHHub;
+  USB_DEVICE_PATHDev;
+  EFI_DEVICE_PATH_PROTOCOL   EndDevicePath;
+} PLATFORM_USB_DEV;
+
+typedef struct {
+  VENDOR_DEVICE_PATH Custom;
+  EFI_DEVICE_PATH_PROTOCOL   EndDevicePath;
+} PLATFORM_SD_DEV;
+
+#define ARASAN_MMC_DXE_FILE_GUID  \
+  { 0x100c2cfa, 0xb586, 0x4198, { 0x9b, 0x4c, 0x16, 0x83, 0xd1, 0x95, 0xb1, 
0xda } }
+
+#define SDHOST_MMC_DXE_FILE_GUID  \
+  { 0x58abd787, 0xf64d, 0x4ca2, { 0xa0, 0x34, 0xb9, 0xac, 0x2d, 0x5a, 0xd0, 
0xcf } }
+
+#define SERIAL_DXE_FILE_GUID  \
+  { 0xD3987D4B, 0x971A, 0x435F, { 0x8C, 0xAF, 0x49, 0x67, 0xEB, 0x62, 0x72, 
0x41 } }
+
+STATIC PLATFORM_SD_DEV mArasan = {
+  //
+  // VENDOR_DEVICE_PATH ArasanMMCHostDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+ARASAN_MMC_DXE_FILE_GUID
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+STATIC PLATFORM_SD_DEV mSDHost = {
+  //
+  // VENDOR_DEVICE_PATH SdHostDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+SDHOST_MMC_DXE_FILE_GUID
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+STATIC PLATFORM_SERIAL_CONSOLE mSerialConsole = {
+  //
+  // VENDOR_DEVICE_PATH SerialDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+SERIAL_DXE_FILE_GUID
+  },
+
+  //
+  // UART_DEVICE_PATH Uart
+  //
+  {
+{ MESSAGING_DEVICE_PATH, MSG_UART_DP, DP_NODE_LEN (UART_DEVICE_PATH) },
+0,  // Reserved
+FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
+FixedPcdGet8 (PcdUartDefaultDataBits),  // DataBits
+FixedPcdGet8 (PcdUartDefaultParity),// Parity
+FixedPcdGet8 (PcdUartDefaultStopBits)   // StopBits
+  },
+
+  //
+  // VENDOR_DEFINED_DEVICE_PATH TermType
+  //
+  {
+{
+  MESSAGING_DEVICE_PATH, MSG_VENDOR_DP,
+  DP_NODE_LEN (VENDOR_DEFINED_DEVICE_PATH)
+}
+//
+// Guid to be filled in dynamically
+//
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+
+#pragma pack (1)
+typedef struct {
+  USB_CLASS_DEVICE_PATHKeyboard;
+  EFI_DEVICE_PATH_PROTOCOL End;
+} PLATFORM_USB_KEYBOARD;
+#pragma pack ()
+
+STATIC PLATFORM_USB_KEYBOARD mUsbKeyboard = {
+  //

[edk2] [PATCH v4 edk2-platforms 16/23] Platform/Raspberry/Pi3: Platform/Raspberry/Pi3: Add SD Host driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c  | 787 

 Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.inf|  55 ++
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836SdHost.h |  92 +++
 3 files changed, 934 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c 
b/Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c
new file mode 100644
index ..3bf789f96b27
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c
@@ -0,0 +1,787 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#define SDHOST_BLOCK_BYTE_LENGTH512
+
+// Driver Timing Parameters
+#define CMD_STALL_AFTER_POLL_US 1
+#define CMD_MIN_POLL_TOTAL_TIME_US  10 // 100ms
+#define CMD_MAX_POLL_COUNT  (CMD_MIN_POLL_TOTAL_TIME_US / 
CMD_STALL_AFTER_POLL_US)
+#define CMD_MAX_RETRY_COUNT 3
+#define CMD_STALL_AFTER_RETRY_US20 // 20us
+#define FIFO_MAX_POLL_COUNT 100
+#define STALL_TO_STABILIZE_US   1 // 10ms
+
+#define IDENT_MODE_SD_CLOCK_FREQ_HZ 40 // 400KHz
+
+// Macros adopted from MmcDxe internal header
+#define SDHOST_R0_READY_FOR_DATABIT8
+#define SDHOST_R0_CURRENTSTATE(Response)((Response >> 9) & 0xF)
+
+#define DEBUG_MMCHOST_SD   DEBUG_VERBOSE
+#define DEBUG_MMCHOST_SD_INFO  DEBUG_INFO
+#define DEBUG_MMCHOST_SD_ERROR DEBUG_ERROR
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL   *mFwProtocol;
+
+// Per Physical Layer Simplified Specs
+#ifndef NDEBUG
+STATIC CONST CHAR8* mStrSdState[] = { "idle", "ready", "ident", "stby",
+  "tran", "data", "rcv", "prg", "dis",
+  "ina" };
+STATIC CONST CHAR8 *mFsmState[] = { "identmode", "datamode", "readdata",
+"writedata", "readwait", "readcrc",
+"writecrc", "writewait1", "powerdown",
+"powerup", "writestart1", "writestart2",
+"genpulses", "writewait2", "?",
+"startpowdown" };
+#endif /* NDEBUG */
+STATIC UINT32 mLastGoodCmd = MMC_GET_INDX (MMC_CMD0);
+
+STATIC inline BOOLEAN
+IsAppCmd (
+  VOID
+  )
+{
+  return mLastGoodCmd == MMC_CMD55;
+}
+
+STATIC BOOLEAN
+IsBusyCmd (
+  IN  UINT32 MmcCmd
+  )
+{
+  if (IsAppCmd ()) {
+return FALSE;
+  }
+
+  return MmcCmd == MMC_CMD7 || MmcCmd == MMC_CMD12;
+}
+
+STATIC BOOLEAN
+IsWriteCmd (
+  IN  UINT32 MmcCmd
+  )
+{
+  if (IsAppCmd ()) {
+return FALSE;
+  }
+
+  return MmcCmd == MMC_CMD24 || MmcCmd == MMC_CMD25;
+}
+
+STATIC BOOLEAN
+IsReadCmd (
+  IN  UINT32 MmcCmd,
+  IN  UINT32 Argument
+  )
+{
+  if (MmcCmd == MMC_CMD8 && !IsAppCmd ()) {
+if (Argument == CMD8_MMC_ARG) {
+  DEBUG ((DEBUG_MMCHOST_SD, "Sending MMC CMD8 variant\n"));
+  return TRUE;
+} else {
+  ASSERT (Argument == CMD8_SD_ARG);
+  DEBUG ((DEBUG_MMCHOST_SD, "Sending SD CMD8 variant\n"));
+  return FALSE;
+}
+  }
+
+  return
+(MmcCmd == MMC_CMD6 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD17 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD18 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD13 && IsAppCmd ()) ||
+(MmcCmd == MMC_ACMD22 && IsAppCmd ()) ||
+(MmcCmd == MMC_ACMD51 && IsAppCmd ());
+}
+
+STATIC VOID
+SdHostDumpRegisters (
+  VOID
+  )
+{
+  DEBUG ((DEBUG_MMCHOST_SD, "SdHost: Registers Dump:\n"));
+  DEBUG ((DEBUG_MMCHOST_SD, "  CMD:  0x%8.8X\n", MmioRead32 (SDHOST_CMD)));
+  DEBUG ((DEBUG_MMCHOST_SD, "  ARG:  0x%8.8X\n", MmioRead32 (SDHOST_ARG)));
+  DEBUG ((DEBUG_MMCHOST_SD, "  TOUT: 0x%8.8X\n", MmioRead32 (SDHOST_TOUT)));
+  DEBUG ((DEBUG_MMCH

[edk2] [PATCH v4 edk2-platforms 15/23] Platform/Raspberry/Pi3: Add Arasan MMC driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c   | 723 

 Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.h   |  50 ++
 Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.inf |  52 ++
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Sdio.h  | 199 
++
 4 files changed, 1024 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c 
b/Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
new file mode 100644
index ..828b40f82a5f
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
@@ -0,0 +1,723 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 "ArasanMmcHostDxe.h"
+
+#define DEBUG_MMCHOST_SD DEBUG_VERBOSE
+
+BOOLEAN PreviousIsCardPresent = FALSE;
+UINT32 LastExecutedCommand = (UINT32) -1;
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+/**
+   These SD commands are optional, according to the SD Spec
+**/
+BOOLEAN
+IgnoreCommand (
+  UINT32 Command
+  )
+{
+  switch (Command) {
+  case MMC_CMD20:
+return TRUE;
+  default:
+return FALSE;
+  }
+}
+
+/**
+   Translates a generic SD command into the format used by the Arasan SD Host 
Controller
+**/
+UINT32
+TranslateCommand (
+  UINT32 Command,
+  UINT32 Argument
+  )
+{
+  UINT32 Translation = 0x;
+
+  if (LastExecutedCommand == CMD55) {
+switch (Command) {
+case MMC_CMD6:
+  Translation = ACMD6;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD6\n"));
+  break;
+case MMC_ACMD22:
+  Translation = ACMD22;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD22\n"));
+  break;
+case MMC_ACMD41:
+  Translation = ACMD41;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD41\n"));
+  break;
+case MMC_ACMD51:
+  Translation = ACMD51;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD51\n"));
+  break;
+default:
+  DEBUG ((DEBUG_ERROR, "ArasanMMCHost: TranslateCommand(): Unrecognized 
App command: %d\n", Command));
+}
+  } else {
+switch (Command) {
+case MMC_CMD0:
+  Translation = CMD0;
+  break;
+case MMC_CMD1:
+  Translation = CMD1;
+  break;
+case MMC_CMD2:
+  Translation = CMD2;
+  break;
+case MMC_CMD3:
+  Translation = CMD3;
+  break;
+case MMC_CMD5:
+  Translation = CMD5;
+  break;
+case MMC_CMD6:
+  Translation = CMD6;
+  break;
+case MMC_CMD7:
+  Translation = CMD7;
+  break;
+case MMC_CMD8: {
+  if (Argument == CMD8_SD_ARG) {
+Translation = CMD8_SD;
+DEBUG ((DEBUG_MMCHOST_SD, "Sending SD CMD8 variant\n"));
+  } else {
+ASSERT (Argument == CMD8_MMC_ARG);
+Translation = CMD8_MMC;
+DEBUG ((DEBUG_MMCHOST_SD, "Sending MMC CMD8 variant\n"));
+  }
+  break;
+}
+case MMC_CMD9:
+  Translation = CMD9;
+  break;
+case MMC_CMD11:
+  Translation = CMD11;
+  break;
+case MMC_CMD12:
+  Translation = CMD12;
+  break;
+case MMC_CMD13:
+  Translation = CMD13;
+  break;
+case MMC_CMD16:
+  Translation = CMD16;
+  break;
+case MMC_CMD17:
+  Translation = CMD17;
+  break;
+case MMC_CMD18:
+  Translation = CMD18;
+  break;
+case MMC_CMD23:
+  Translation = CMD23;
+  break;
+case MMC_CMD24:
+  Translation = CMD24;
+  break;
+case MMC_CMD25:
+  Translation = CMD25;
+  break;
+case MMC_CMD55:
+  Translation = CMD55;
+  break;
+default:
+  DEBUG ((DEBUG_ERROR, "ArasanMMCHost: TranslateCommand(): Unrecognized 
Command: %d\n", Command));
+}
+  }
+
+  return Translation;
+}
+
+/**
+   Repeatedly polls a register until its value becomes correct, or until 
MAX_RETRY_COUNT polls is reached
+**/
+EFI_STATUS
+PollRegisterWithMask (
+  IN UINTN Register,
+  IN UINTN Mask,
+  IN UINTN ExpectedValue
+  )
+{
+  UINTN RetryCount = 0;
+
+  while (RetryCount < MAX_RETRY_COUNT) {
+if ((MmioRead32 (Register) & Mask) != ExpectedValue) {
+  RetryCount++;
+  gBS->Stall (STALL_AFTER_RETRY_US);
+} else {
+  break;
+}
+  }
+
+  if (RetryCount == MAX_RETRY_COUNT) {
+return EFI_TIMEOUT;
+  }
+
+  return EFI_SUCCESS;
+}
+
+STATIC
+E

[edk2] [PATCH v4 edk2-platforms 14/23] Platform/Raspberry/Pi3: Add base MMC driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c | 163 
 Platform/Raspberry/Pi3/Drivers/MmcDxe/Diagnostics.c   | 263 ++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/Mmc.c   | 460 +
 Platform/Raspberry/Pi3/Drivers/MmcDxe/Mmc.h   | 533 +++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcBlockIo.c| 469 ++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcDebug.c  | 170 
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcDxe.inf  |  58 ++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcIdentification.c | 980 

 Platform/Raspberry/Pi3/Include/Protocol/RpiMmcHost.h  | 206 
 9 files changed, 3302 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c
new file mode 100644
index ..034da778cae2
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c
@@ -0,0 +1,163 @@
+/** @file
+ *
+ *  Component Name Protocol implementation for the MMC DXE driver
+ *
+ *  Copyright (c) 2011, ARM Limited. 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 "Mmc.h"
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL  gMmcComponentName = 
{
+  MmcGetDriverName,
+  MmcGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gMmcComponentName2 
= {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)MmcGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)MmcGetControllerName,
+  "en"
+};
+
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE
+mMmcDriverNameTable[] = {
+  {"eng;en", L"MMC/SD Card Interface Driver"},
+  {NULL,  NULL}
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+  @param  Language  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+  @param  DriverNameA pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+EFI_STATUS
+EFIAPI
+MmcGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+   Language,
+   This->SupportedLanguages,
+   mMmcDriverNameTable,
+   DriverName,
+   (BOOLEAN)(This == &gMmcComponentName)
+ );
+}
+
+/**
+  Retrieves a Unicode string that is the user readable name of the controller
+  that is being managed by a driver.
+
+  This function retrieves the user readable name of the controller specified by

[edk2] [PATCH v4 edk2-platforms 13/23] Platform/Raspberry/Pi3: Add Device Tree driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c   | 364 
 Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.inf |  53 +++
 2 files changed, 417 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c 
b/Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c
new file mode 100644
index ..eb5698cb505b
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c
@@ -0,0 +1,364 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrey Warkentin 
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+
+STATIC VOID *mFdtImage;
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL   *mFwProtocol;
+
+STATIC
+VOID
+UpdateMacAddress (
+  VOID
+  )
+{
+  INTN  Node;
+  INTN  Retval;
+  EFI_STATUSStatus;
+  UINT8 MacAddress[6];
+
+  //
+  // Locate the node that the 'ethernet' alias refers to
+  //
+  Node = fdt_path_offset (mFdtImage, "ethernet");
+  if (Node < 0) {
+DEBUG ((DEBUG_ERROR, "%a: failed to locate 'ethernet' alias\n", 
__FUNCTION__));
+return;
+  }
+
+  //
+  // Get the MAC address from the firmware
+  //
+  Status = mFwProtocol->GetMacAddress (MacAddress);
+  if (EFI_ERROR (Status)) {
+DEBUG ((DEBUG_ERROR, "%a: failed to retrieve MAC address\n", 
__FUNCTION__));
+return;
+  }
+
+  Retval = fdt_setprop (mFdtImage, Node, "mac-address", MacAddress,
+sizeof MacAddress);
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "%a: failed to create 'mac-address' property (%d)\n",
+  __FUNCTION__, Retval));
+return;
+  }
+
+  DEBUG ((DEBUG_INFO, "%a: setting MAC address to 
%02x:%02x:%02x:%02x:%02x:%02x\n",
+__FUNCTION__, MacAddress[0], MacAddress[1], MacAddress[2], MacAddress[3],
+MacAddress[4], MacAddress[5]));
+}
+
+STATIC
+VOID
+CleanMemoryNodes (
+  VOID
+  )
+{
+  INTN Node;
+  INT32 Retval;
+
+  Node = fdt_path_offset (mFdtImage, "/memory");
+  if (Node < 0) {
+return;
+  }
+
+  /*
+   * Remove bogus memory nodes which can make the booted
+   * OS go crazy and ignore the UEFI map.
+   */
+  DEBUG ((DEBUG_INFO, "Removing bogus /memory\n"));
+  Retval = fdt_del_node (mFdtImage, Node);
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Failed to remove /memory\n"));
+  }
+}
+
+STATIC
+VOID
+SanitizePSCI (
+  VOID
+  )
+{
+  INTN Node;
+  INTN Root;
+  INT32 Retval;
+
+  Root = fdt_path_offset (mFdtImage, "/");
+  ASSERT (Root >= 0);
+  if (Root < 0) {
+return;
+  }
+
+  Node = fdt_path_offset (mFdtImage, "/psci");
+  if (Node < 0) {
+Node = fdt_add_subnode (mFdtImage, Root, "psci");
+  }
+
+  ASSERT (Node >= 0);
+  if (Node < 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't find/create /psci\n"));
+return;
+  }
+
+  Retval = fdt_setprop_string (mFdtImage, Node, "compatible", "arm,psci-1.0");
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't set /psci compatible property\n"));
+return;
+  }
+
+  Retval = fdt_setprop_string (mFdtImage, Node, "method", "smc");
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't set /psci method property\n"));
+return;
+  }
+
+  Root = fdt_path_offset (mFdtImage, "/cpus");
+  if (Root < 0) {
+DEBUG ((DEBUG_ERROR, "No CPUs to update with PSCI enable-method?\n"));
+return;
+  }
+
+  Node = fdt_first_subnode (mFdtImage, Root);
+  while (Node >= 0) {
+if (fdt_setprop_string (mFdtImage, Node, "enable-method", "psci") != 0) {
+  DEBUG ((DEBUG_ERROR, "Failed to update enable-method for a CPU\n"));
+  return;
+}
+
+fdt_delprop (mFdtImage, Node, "cpu-release-addr");
+Node = fdt_next_subnode (mFdtImage, Node);
+  }
+}
+
+STATIC
+VOID
+CleanSimpleFramebuffer (
+  VOID
+  )
+{
+  INTN Node;
+  INT32 Retval;
+
+  /*
+   * Should look for nodes by kind and remove aliases
+   * by matching against device.
+   */
+  Node = fdt_path_offset (mFdtImage, "display0");
+  if (Node < 0) {
+return;
+  }
+
+  /*
+   * Remove bogus GPU-injected simple-framebuffer, which
+   * doesn't reflect the framebuffer built by UEFI.
+   */
+  DEBUG ((DEBUG_INF

[edk2] [PATCH v4 edk2-platforms 12/23] Platform/Raspberry/Pi3: Add NV storage driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c   | 196 

 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FvbInfo.c  | 115 
+++
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockService.c  | 971 

 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockService.h  | 217 
+
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.c   | 331 
+++
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.inf |  93 
++
 6 files changed, 1923 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c 
b/Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c
new file mode 100644
index ..0e8cd516f65e
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c
@@ -0,0 +1,196 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2007-2009, Intel Corporation. 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 "VarBlockService.h"
+
+
+EFI_STATUS
+FileWrite (
+  IN EFI_FILE_PROTOCOL *File,
+  IN UINTN Offset,
+  IN UINTN Buffer,
+  IN UINTN Size
+  )
+{
+  EFI_STATUS Status;
+
+  Status = File->SetPosition (File, Offset);
+  ASSERT_EFI_ERROR (Status);
+  if (!EFI_ERROR (Status)) {
+Status = File->Write (File, &Size, (VOID*)Buffer);
+ASSERT_EFI_ERROR (Status);
+  }
+  return Status;
+}
+
+
+VOID
+FileClose (
+  IN  EFI_FILE_PROTOCOL *File
+  )
+{
+  File->Flush (File);
+  File->Close (File);
+}
+
+
+EFI_STATUS
+FileOpen (
+  IN  EFI_DEVICE_PATH_PROTOCOL *Device,
+  IN  CHAR16 *MappedFile,
+  OUT EFI_FILE_PROTOCOL **File,
+  IN  UINT64 OpenMode
+  )
+{
+  EFI_HANDLEHandle;
+  EFI_FILE_HANDLE   Root;
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL   *Volume;
+  EFI_STATUSStatus;
+
+  *File = NULL;
+
+  Status = gBS->LocateDevicePath (
+  &gEfiSimpleFileSystemProtocolGuid,
+  &Device,
+  &Handle
+);
+
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  Status = gBS->HandleProtocol (
+  Handle,
+  &gEfiSimpleFileSystemProtocolGuid,
+  (VOID**)&Volume
+);
+  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  //
+  // Open the root directory of the volume
+  //
+  Root = NULL;
+  Status = Volume->OpenVolume (
+ Volume,
+ &Root
+   );
+  ASSERT_EFI_ERROR (Status);
+  ASSERT (Root != NULL);
+
+  //
+  // Open file
+  //
+  Status = Root->Open (
+   Root,
+   File,
+   MappedFile,
+   OpenMode,
+   0
+ );
+  if (EFI_ERROR (Status)) {
+*File = NULL;
+  }
+
+  //
+  // Close the Root directory
+  //
+  Root->Close (Root);
+  return Status;
+}
+
+
+EFI_STATUS
+CheckStore (
+  IN  EFI_HANDLE SimpleFileSystemHandle,
+  OUT EFI_DEVICE_PATH_PROTOCOL **Device
+  )
+{
+  EFI_STATUS Status;
+  EFI_BLOCK_IO_PROTOCOL *BlkIo;
+  EFI_FILE_PROTOCOL *File;
+
+  *Device = NULL;
+  Status = gBS->HandleProtocol (
+  SimpleFileSystemHandle,
+  &gEfiBlockIoProtocolGuid,
+  (VOID*)&BlkIo
+);
+
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+  if (!BlkIo->Media->MediaPresent) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media not present!\n"));
+Status = EFI_NO_MEDIA;
+goto ErrHandle;
+  }
+  if (BlkIo->Media->ReadOnly) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media is read-only!\n"));
+Status = EFI_ACCESS_DENIED;
+goto ErrHandle;
+  }
+
+  Status = FileOpen (DevicePathFromHandle (SimpleFileSystemHandle),
+ mFvInstance->MappedFile, &File,
+ EFI_FILE_MODE_READ);
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+
+  /* We found it! Maybe do more checks...? */
+
+  FileClose (File);
+  *Device = DuplicateDevicePath (DevicePathFromHandle 
(SimpleFileSystemHandle));
+
+  ASSERT (*Device != NULL);
+
+ErrHandle:
+  return Status;
+}
+
+
+EFI_STATUS
+CheckStoreExists (
+  IN  EFI_DEVICE_PATH_PROTOCOL *Device
+  )
+{
+  EFI_HANDLE Handle;
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
+  EFI_STATUS Status;
+
+  Status = gBS->

[edk2] [PATCH v4 edk2-platforms 11/23] Platform/Raspberry/Pi3: Add console driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c 
|  183 ++
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsole.c   
| 1837 
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsole.h   
|  591 +++
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxe.inf  
|   75 +
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxe.uni  
|   18 +
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxeExtra.uni 
|   18 +
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/NewFont.c   
|  287 +++
 Platform/Raspberry/Pi3/Include/Protocol/ExtendedTextOut.h 
|   36 +
 8 files changed, 3045 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c
new file mode 100644
index ..3ae639ad3d97
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c
@@ -0,0 +1,183 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2006-2016, Intel Corporation. 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 "GraphicsConsole.h"
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL  
gGraphicsConsoleComponentName = {
+  GraphicsConsoleComponentNameGetDriverName,
+  GraphicsConsoleComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL 
gGraphicsConsoleComponentName2 = {
+  
(EFI_COMPONENT_NAME2_GET_DRIVER_NAME)GraphicsConsoleComponentNameGetDriverName,
+  
(EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)GraphicsConsoleComponentNameGetControllerName,
+  "en"
+};
+
+
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE 
mGraphicsConsoleDriverNameTable[] = {
+  {
+"eng;en",
+(CHAR16*)L"Graphics Console Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+EFI_STATUS
+EFIAPI
+GraphicsConsoleComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+   Language,
+   This->SupportedLanguages,
+   

[edk2] [PATCH v4 edk2-platforms 10/23] Platform/Raspberry/Pi3: Add display driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c | 222 +++
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/DisplayDxe.c| 606 

 Platform/Raspberry/Pi3/Drivers/DisplayDxe/DisplayDxe.h|  42 ++
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/DisplayDxe.inf  |  71 +++
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/Screenshot.c| 375 
 5 files changed, 1316 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c
new file mode 100644
index ..9a84aea511f4
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c
@@ -0,0 +1,222 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2006-2016, Intel Corporation. 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 "DisplayDxe.h"
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  );
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL *This,
+  IN  EFI_HANDLE  ControllerHandle,
+  IN  EFI_HANDLE  ChildHandle,
+  IN  CHAR8   *Language,
+  OUT CHAR16  **ControllerName
+  );
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
+  ComponentNameGetDriverName,
+  ComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)ComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)ComponentNameGetControllerName,
+  "en"
+};
+
+
+STATIC EFI_UNICODE_STRING_TABLE mDriverName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi Display Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mDeviceName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi Framebuffer"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT 

[edk2] [PATCH v4 edk2-platforms 09/23] Platform/Raspberry/Pi3: Add SMBIOS driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c   | 903 

 Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.inf |  56 ++
 2 files changed, 959 insertions(+)

diff --git 
a/Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c 
b/Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c
new file mode 100644
index ..7707763e4332
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c
@@ -0,0 +1,903 @@
+/** @file
+ *
+ *  Static SMBIOS Table for ARM platform
+ *  Derived from EmulatorPkg package
+ *
+ *  Note SMBIOS 2.7.1 Required structures:
+ *  BIOS Information (Type 0)
+ *  System Information (Type 1)
+ *  Board Information (Type 2)
+ *  System Enclosure (Type 3)
+ *  Processor Information (Type 4) - CPU Driver
+ *  Cache Information (Type 7) - For cache that is external to processor
+ *  System Slots (Type 9) - If system has slots
+ *  Physical Memory Array (Type 16)
+ *  Memory Device (Type 17) - For each socketed system-memory Device
+ *  Memory Array Mapped Address (Type 19) - One per contiguous block per 
Physical Memroy Array
+ *  System Boot Information (Type 32)
+ *
+ *  Copyright (c) 2017-2018, Andrey Warkentin 
+ *  Copyright (c) 2013, Linaro.org
+ *  Copyright (c) 2012, Apple Inc. All rights reserved.
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+/***
+SMBIOS data definition  TYPE0  BIOS Information
+/
+SMBIOS_TABLE_TYPE0 mBIOSInfoType0 = {
+  { EFI_SMBIOS_TYPE_BIOS_INFORMATION, sizeof (SMBIOS_TABLE_TYPE0), 0 },
+  1,// Vendor String
+  2,// BiosVersion String
+  0x0,  // BiosSegment
+  3,// BiosReleaseDate String
+  0x1F, // BiosSize
+  { // BiosCharacteristics
+0,//  Reserved  :2;  ///< Bits 0-1.
+0,//  Unknown   :1;
+0,//  BiosCharacteristicsNotSupported   :1;
+0,//  IsaIsSupported:1;
+0,//  McaIsSupported:1;
+0,//  EisaIsSupported   :1;
+0,//  PciIsSupported:1;
+0,//  PcmciaIsSupported :1;
+0,//  PlugAndPlayIsSupported:1;
+0,//  ApmIsSupported:1;
+0,//  BiosIsUpgradable  :1;
+0,//  BiosShadowingAllowed  :1;
+0,//  VlVesaIsSupported :1;
+0,//  EscdSupportIsAvailable:1;
+0,//  BootFromCdIsSupported :1;
+1,//  SelectableBootIsSupported :1;
+0,//  RomBiosIsSocketed :1;
+0,//  BootFromPcmciaIsSupported :1;
+0,//  EDDSpecificationIsSupported   :1;
+0,//  JapaneseNecFloppyIsSupported  :1;
+0,//  JapaneseToshibaFloppyIsSupported  :1;
+0,//  Floppy525_360IsSupported  :1;
+0,//  Floppy525_12IsSupported   :1;
+0,//  Floppy35_720IsSupported   :1;
+0,//  Floppy35_288IsSupported   :1;
+0,//  PrintScreenIsSupported:1;
+0,//  Keyboard8042IsSupported   :1;
+0,//  SerialIsSupported :1;
+0,//  PrinterIsSupported:1;
+0,//  CgaMonoIsSupported:1;
+0,//  NecPc98   :1;
+0 //  ReservedForVendor :32; ///< Bits 32-63. Bits 
32-47 reserved for BIOS vendor
+///< and bits 48-63 reserved for System Vendor.
+  },
+  {   // BIOSCharacteristicsExtensionBytes[]
+0x01, //  AcpiIsSupported   :1;
+  //  UsbLegacyIsSupported  :1;
+  //  AgpIsSupported:1;
+  //  I2OBootIsSupported:1;
+  //  Ls120BootIsSupported  :1;
+  //  AtapiZipDriveBootIsSupported  :1;
+  //  

[edk2] [PATCH v4 edk2-platforms 08/23] Platform/Raspberry/Pi3: Add platform config driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c| 351 

 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.inf  |  78 +
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxeFormSetGuid.h |  23 ++
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxeHii.uni   | 100 ++
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxeHii.vfr   | 306 
+
 5 files changed, 858 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c 
b/Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c
new file mode 100644
index ..b78d7deae402
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c
@@ -0,0 +1,351 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "ConfigDxeFormSetGuid.h"
+
+extern UINT8 ConfigDxeHiiBin[];
+extern UINT8 ConfigDxeStrings[];
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+typedef struct {
+  VENDOR_DEVICE_PATH VendorDevicePath;
+  EFI_DEVICE_PATH_PROTOCOL End;
+} HII_VENDOR_DEVICE_PATH;
+
+STATIC HII_VENDOR_DEVICE_PATH mVendorDevicePath = {
+  {
+{
+  HARDWARE_DEVICE_PATH,
+  HW_VENDOR_DP,
+  {
+(UINT8)(sizeof (VENDOR_DEVICE_PATH)),
+(UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
+  }
+},
+CONFIGDXE_FORM_SET_GUID
+  },
+  {
+END_DEVICE_PATH_TYPE,
+END_ENTIRE_DEVICE_PATH_SUBTYPE,
+{
+  (UINT8)(END_DEVICE_PATH_LENGTH),
+  (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
+}
+  }
+};
+
+
+STATIC EFI_STATUS
+InstallHiiPages (
+  VOID
+  )
+{
+  EFI_STATUS Status;
+  EFI_HII_HANDLE HiiHandle;
+  EFI_HANDLE DriverHandle;
+
+  DriverHandle = NULL;
+  Status = gBS->InstallMultipleProtocolInterfaces (&DriverHandle,
+  &gEfiDevicePathProtocolGuid,
+  &mVendorDevicePath,
+  NULL);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  HiiHandle = HiiAddPackages (&gConfigDxeFormSetGuid,
+DriverHandle,
+ConfigDxeStrings,
+ConfigDxeHiiBin,
+NULL);
+
+  if (HiiHandle == NULL) {
+gBS->UninstallMultipleProtocolInterfaces (DriverHandle,
+   &gEfiDevicePathProtocolGuid,
+   &mVendorDevicePath,
+   NULL);
+return EFI_OUT_OF_RESOURCES;
+  }
+  return EFI_SUCCESS;
+}
+
+
+STATIC EFI_STATUS
+SetupVariables (
+  VOID
+  )
+{
+  UINTN Size;
+  UINT32 Var32;
+  EFI_STATUS Status;
+
+  /*
+   * Create the vars with default value.
+   * If we don't, forms won't be able to update.
+   */
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"CpuClock",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdCpuClock, PcdGet32 (PcdCpuClock));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"SdIsArasan",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdSdIsArasan, PcdGet32 (PcdSdIsArasan));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcDisableMulti",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcDisableMulti, PcdGet32 (PcdMmcDisableMulti));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcForce1Bit",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForce1Bit, PcdGet32 (PcdMmcForce1Bit));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcForceDefaultSpeed",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForceDefaultSpeed, PcdGet32 (PcdMmcForceDefaultSpeed));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcSdDefaultSpeedMHz",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcSdDefaultSpeedMHz, PcdGet32 (PcdMmcSdDefaultSp

[edk2] [PATCH v4 edk2-platforms 07/23] Platform/Raspberry/Pi3: Add firmware driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 

Reviewed-by: Ard Biesheuvel 
---
 Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c   | 1084 

 Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.inf |   50 +
 Platform/Raspberry/Pi3/Include/Protocol/RpiFirmware.h|  131 +++
 3 files changed, 1265 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c 
b/Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
new file mode 100644
index ..d330e45fdc4c
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
@@ -0,0 +1,1084 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrei Warkentin 
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include 
+
+//
+// The number of statically allocated buffer pages
+//
+#define NUM_PAGES   1
+
+//
+// The number of iterations to perform when waiting for the mailbox
+// status to change
+//
+#define MAX_TRIES   0x10
+
+STATIC VOID  *mDmaBuffer;
+STATIC VOID  *mDmaBufferMapping;
+STATIC UINTN mDmaBufferBusAddress;
+
+STATIC SPIN_LOCK mMailboxLock;
+
+STATIC
+BOOLEAN
+DrainMailbox (
+  VOID
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if (Val & (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+BOOLEAN
+MailboxWaitForStatusCleared (
+  IN  UINTN   StatusMask
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if ((Val & StatusMask) == 0) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+EFI_STATUS
+MailboxTransaction (
+  INUINTN   Length,
+  INUINTN   Channel,
+  OUT   UINT32  *Result
+  )
+{
+  if (Channel >= BCM2836_MBOX_NUM_CHANNELS) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  if (!DrainMailbox ()) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for mailbox to drain\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Wait for the 'output register full' bit to become clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_FULL)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for outbox to become empty\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Start the mailbox transaction
+  //
+  MmioWrite32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_WRITE_OFFSET,
+(UINT32)((UINTN)mDmaBufferBusAddress | Channel));
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Wait for the 'input register empty' bit to clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for inbox to become full\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Read back the result
+  //
+  ArmDataSynchronizationBarrier ();
+  *Result = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  ArmDataSynchronizationBarrier ();
+
+  return EFI_SUCCESS;
+}
+
+#pragma pack(1)
+typedef struct {
+  UINT32BufferSize;
+  UINT32Response;
+} RPI_FW_BUFFER_HEAD;
+
+typedef struct {
+  UINT32TagId;
+  UINT32TagSize;
+  UINT32TagValueSize;
+} RPI_FW_TAG_HEAD;
+
+typedef struct {
+  UINT32DeviceId;
+  UINT32PowerState;
+} RPI_FW_POWER_STATE_TAG;
+
+typedef struct {
+  RPI_FW_BUFFER_HEADBufferHead;
+  RPI_FW_TAG_HEAD   TagHead;
+  RPI_FW_POWER_STATE_TAGTagBody;
+  UINT32EndTag;
+} RPI_FW_SET_POWER_STATE_CMD;
+#pragma pack()
+
+STATIC
+EFI_STATUS
+EFIAPI
+RpiFirmwareSetPowerState (
+  IN  UINT32DeviceId,
+  IN  BOOLEAN   PowerState,
+  IN  BOOLEAN   Wai

[edk2] [PATCH v4 edk2-platforms 06/23] Platform/Raspberry/Pi3: Add RTC library

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 
Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
   | 221 
 
Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
 |  43 
 2 files changed, 264 insertions(+)

diff --git 
a/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
 
b/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
new file mode 100644
index ..dea621ff03ef
--- /dev/null
+++ 
b/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
@@ -0,0 +1,221 @@
+/** @file
+ *
+ *  Implement dummy EFI RealTimeClock runtime services.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/**
+   Returns the current time and date information, and the time-keeping 
capabilities
+   of the virtual RTC.
+
+   For simplicity, this LibGetTime does not report Years/Months, instead it 
will only report current
+   Day, Hours, Minutes and Seconds starting from the begining of CPU up-time. 
Otherwise, a more
+   complex logic will be required to account for leap years and days/month 
differences.
+
+   @param  Time  A pointer to storage to receive a snapshot of 
the current time.
+   @param  Capabilities  An optional pointer to a buffer to receive 
the real time clock
+   device's capabilities.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER Time is NULL.
+   @retval EFI_DEVICE_ERROR  The time could not be retrieved due to 
hardware error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibGetTime (
+  OUT EFI_TIME   *Time,
+  OUT EFI_TIME_CAPABILITIES  *Capabilities
+  )
+{
+  UINTN DataSize;
+  UINT64 Counter;
+  EFI_STATUS Status;
+  UINTN ElapsedSeconds;
+  UINT32 Remainder;
+  UINT32 Freq = ArmGenericTimerGetTimerFreq ();
+
+  if (Time == NULL) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Depend on ARM generic timer to report date/time relative to the
+  // start of CPU timer counting where date and time will always
+  // be relative to the date/time 1/1/1900 00H:00M:00S
+  //
+
+  ASSERT (Freq != 0);
+  if (Freq == 0) {
+return EFI_DEVICE_ERROR;
+  }
+
+  if (Capabilities) {
+Capabilities->Accuracy = 0;
+Capabilities->Resolution = Freq;
+Capabilities->SetsToZero = FALSE;
+  }
+
+  DataSize = sizeof (UINTN);
+  ElapsedSeconds = 0;
+  Status = EfiGetVariable (L"RtcEpochSeconds",
+ &gEfiCallerIdGuid,
+ NULL,
+ &DataSize,
+ &ElapsedSeconds);
+  if (EFI_ERROR (Status)) {
+ElapsedSeconds = PcdGet64 (PcdBootEpochSeconds);
+  }
+  Counter = GetPerformanceCounter ();
+  ElapsedSeconds += DivU64x32Remainder (Counter, Freq, &Remainder);
+  EpochToEfiTime (ElapsedSeconds, Time);
+
+  //
+  // Frequency < 0x1, so Remainder < 0x1, then (Remainder * 
1,000,000,000)
+  // will not overflow 64-bit.
+  //
+  Time->Nanosecond = DivU64x32 (MultU64x64 ((UINT64)Remainder, 10U), 
Freq);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+   Sets the current local time and date information.
+
+   @param  Time  A pointer to the current time.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER A time field is out of range.
+   @retval EFI_DEVICE_ERROR  The time could not be set due due to hardware 
error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibSetTime (
+  IN EFI_TIME  *Time
+  )
+{
+  UINTN Epoch;
+
+  if (!IsTimeValid (Time)) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  Epoch = EfiTimeToEpoch (Time);
+  return EfiSetVariable (L"RtcEpochSeconds", &gEfiCallerIdGuid,
+   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+   EFI_VARIABLE_RUNTIME_ACCESS |
+   EFI_VARIABLE_NON_VOLATILE,
+   sizeof (Epoch),
+   &Epoch);
+}
+
+
+/**
+   Returns the current wakeup alarm clock setting.
+
+   @param  Enabled   Indicates if the alarm is currently enabled 
or disabled.
+   @param  Pending   Indicates if the alarm signal is pending and 
requires acknowledgement.
+   @param  Time  The current alarm setting.
+
+   @retval EFI_SUCCESS   The alarm se

[edk2] [PATCH v4 edk2-platforms 05/23] Platform/Raspberry/Pi3: Add platform library

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h  | 108 
+
 Platform/Raspberry/Pi3/Library/PlatformLib/AArch64/RaspberryPiHelper.S | 107 
+
 Platform/Raspberry/Pi3/Library/PlatformLib/PlatformLib.inf |  65 

 Platform/Raspberry/Pi3/Library/PlatformLib/RaspberryPi.c   |  99 

 Platform/Raspberry/Pi3/Library/PlatformLib/RaspberryPiMem.c| 160 

 5 files changed, 539 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h 
b/Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h
new file mode 100644
index ..8547ad05ba61
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h
@@ -0,0 +1,108 @@
+/** @file
+ *
+ * Copyright (c) 2019, Pete Batard 
+ * Copyright (c) 2016, Linaro Limited. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * Neither the name of ARM nor the names of its contributors may be used
+ * to endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **/
+
+#ifndef __RASPBERRY_PI_MAILBOX_H__
+#define __RASPBERRY_PI_MAILBOX_H__
+
+/* Mailbox channels */
+#define RPI_MBOX_PM_CHANNEL   0
+#define RPI_MBOX_FB_CHANNEL   1
+#define RPI_MBOX_VUART_CHANNEL2
+#define RPI_MBOX_VCHIQ_CHANNEL3
+#define RPI_MBOX_LED_CHANNEL  4
+#define RPI_MBOX_BUTTON_CHANNEL   5
+#define RPI_MBOX_TOUCHSCREEN_CHANNEL  6
+/* Request from ARM for response by VideoCore */
+#define RPI_MBOX_VC_CHANNEL   8
+/* Request from VideoCore for response by ARM */
+#define RPI_MBOX_ARM_CHANNEL  9
+
+#define RPI_MBOX_RESP_SUCCESS 0x8000
+#define RPI_MBOX_RESP_FAILURE 0x8001
+
+#define RPI_MBOX_VALUE_SIZE_RESPONSE_MASK BIT31
+
+#define RPI_MBOX_GET_REVISION 0x0001
+#define RPI_MBOX_GET_BOARD_MODEL  0x00010001
+#define RPI_MBOX_GET_BOARD_REVISION   0x00010002
+#define RPI_MBOX_GET_MAC_ADDRESS  0x00010003
+#define RPI_MBOX_GET_BOARD_SERIAL 0x00010004
+#define RPI_MBOX_GET_ARM_MEMSIZE  0x00010005
+
+#define RPI_MBOX_SET_POWER_STATE  0x00028001
+
+#define RPI_MBOX_POWER_STATE_SDHCI0x
+#define RPI_MBOX_POWER_STATE_UART00x0001
+#define RPI_MBOX_POWER_STATE_UART10x0002
+#define RPI_MBOX_POWER_STATE_USB_HCD  0x0003
+#define RPI_MBOX_POWER_STATE_I2C0 0x0004
+#define RPI_MBOX_POWER_STATE_I2C1 0x0005
+#define RPI_MBOX_POWER_STATE_I2C2 0x0006
+#define RPI_MBOX_POWER_STATE_SPI  0x0007
+#define RPI_MBOX_POWER_STATE_CCP2TX   0x0008
+
+#define RPI_MBOX_GET_CLOCK_RATE   0x00030002
+#define RPI_MBOX_GET_MAX_CLOCK_RATE   0x00030004
+#define RPI_MBOX_GET_MIN_CLOCK_RATE   0x00030007
+
+#define RPI_MBOX_SET_CLOCK_RATE   

[edk2] [PATCH v4 edk2-platforms 03/23] Platform/Raspberry/Pi3: Add ACPI tables

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h   |  82 
 Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf |  46 ++
 Platform/Raspberry/Pi3/AcpiTables/Csrt.aslc  | 332 +
 Platform/Raspberry/Pi3/AcpiTables/Dbg2.aslc  |  34 ++
 Platform/Raspberry/Pi3/AcpiTables/Dsdt.asl   | 511 
 Platform/Raspberry/Pi3/AcpiTables/Fadt.aslc  |  52 ++
 Platform/Raspberry/Pi3/AcpiTables/Gtdt.aslc  |  33 ++
 Platform/Raspberry/Pi3/AcpiTables/Madt.aslc  |  62 +++
 Platform/Raspberry/Pi3/AcpiTables/Pep.asl|  95 
 Platform/Raspberry/Pi3/AcpiTables/Pep.c  |  84 
 Platform/Raspberry/Pi3/AcpiTables/Pep.h  | 126 +
 Platform/Raspberry/Pi3/AcpiTables/Rhpx.asl   | 201 
 Platform/Raspberry/Pi3/AcpiTables/Sdhc.asl   | 105 
 Platform/Raspberry/Pi3/AcpiTables/Spcr.asl   |  53 ++
 Platform/Raspberry/Pi3/AcpiTables/Uart.asl   | 158 ++
 15 files changed, 1974 insertions(+)

diff --git a/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h 
b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h
new file mode 100644
index ..be28b6decefd
--- /dev/null
+++ b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h
@@ -0,0 +1,82 @@
+/** @file
+ *
+ *  RPi3 defines for constructing ACPI tables
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ **/
+
+#ifndef __ACPITABLES_H__
+#define __ACPITABLES_H__
+
+#include 
+
+#define EFI_ACPI_OEM_ID   {'M','C','R','S','F','T'} // 
OEMID 6 bytes long
+#define EFI_ACPI_OEM_TABLE_ID SIGNATURE_64 
('R','P','I','3','E','D','K','2') // OEM table id 8 bytes long
+#define EFI_ACPI_OEM_REVISION 0x02000820
+#define EFI_ACPI_CREATOR_ID   SIGNATURE_32 ('R','P','I','3')
+#define EFI_ACPI_CREATOR_REVISION 0x0097
+
+#define EFI_ACPI_VENDOR_IDSIGNATURE_32 ('M','S','F','T')
+#define EFI_ACPI_CSRT_REVISION0x0005
+#define EFI_ACPI_CSRT_DEVICE_ID_DMA   0x0009 // Fixed id
+#define EFI_ACPI_CSRT_RESOURCE_ID_IN_DMA_GRP  0x0 // Count up from 0
+
+#define RPI3_DMA_CHANNEL_COUNT10 // All 10 DMA channels are 
listed, including the reserved ones
+#define RPI3_DMA_USED_CHANNEL_COUNT   5  // Use 5 DMA channels
+
+#define EFI_ACPI_5_0_CSRT_REVISION0x
+
+typedef enum
+{
+  EFI_ACPI_CSRT_RESOURCE_TYPE_RESERVED,   // 0
+  EFI_ACPI_CSRT_RESOURCE_TYPE_INTERRUPT,  // 1
+  EFI_ACPI_CSRT_RESOURCE_TYPE_TIMER,  // 2
+  EFI_ACPI_CSRT_RESOURCE_TYPE_DMA,// 3
+  EFI_ACPI_CSRT_RESOURCE_TYPE_CACHE,  // 4
+}
+CSRT_RESOURCE_TYPE;
+
+typedef enum
+{
+  EFI_ACPI_CSRT_RESOURCE_SUBTYPE_DMA_CHANNEL, // 0
+  EFI_ACPI_CSRT_RESOURCE_SUBTYPE_DMA_CONTROLLER   // 1
+}
+CSRT_DMA_SUBTYPE;
+
+//
+// CSRT Resource Group header 24 bytes long
+//
+typedef struct
+{
+  UINT32 Length;  // Length
+  UINT32 VendorID;// 4 bytes
+  UINT32 SubVendorId; // 4 bytes
+  UINT16 DeviceId;// 2 bytes
+  UINT16 SubdeviceId; // 2 bytes
+  UINT16 Revision;// 2 bytes
+  UINT16 Reserved;// 2 bytes
+  UINT32 SharedInfoLength;// 4 bytes
+} EFI_ACPI_5_0_CSRT_RESOURCE_GROUP_HEADER;
+
+//
+// CSRT Resource Descriptor 12 bytes total
+//
+typedef struct
+{
+  UINT32 Length;  // 4 bytes
+  UINT16 ResourceType;// 2 bytes
+  UINT16 ResourceSubType; // 2 bytes
+  UINT32 UID; // 4 bytes
+} EFI_ACPI_5_0_CSRT_RESOURCE_DESCRIPTOR_HEADER;
+
+#endif // __ACPITABLES_H__
diff --git a/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf 
b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf
new file mode 100644
index ..62de9c072052
--- /dev/null
+++ b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf

[edk2] [PATCH v4 edk2-platforms 04/23] Platform/Raspberry/Pi3: Add reset and memory init libraries

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c   | 162 

 Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf |  51 
++
 Platform/Raspberry/Pi3/Library/ResetLib/ResetLib.c   | 104 
+
 Platform/Raspberry/Pi3/Library/ResetLib/ResetLib.inf |  46 
++
 4 files changed, 363 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c 
b/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
new file mode 100644
index ..903364e08b15
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
@@ -0,0 +1,162 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrey Warkentin 
+ *  Copyright (c) 2011-2015, ARM Limited. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+extern UINT64 mSystemMemoryEnd;
+
+VOID
+BuildMemoryTypeInformationHob (
+  VOID
+  );
+
+STATIC
+VOID
+InitMmu (
+  IN ARM_MEMORY_REGION_DESCRIPTOR  *MemoryTable
+  )
+{
+  RETURN_STATUS Status;
+
+  //Note: Because we called PeiServicesInstallPeiMemory() before to call 
InitMmu() the MMU Page Table
+  //  resides in DRAM (even at the top of DRAM as it is the first 
permanent memory allocation)
+  Status = ArmConfigureMmu (MemoryTable, NULL, NULL);
+  if (EFI_ERROR (Status)) {
+DEBUG ((DEBUG_ERROR, "Error: Failed to enable MMU\n"));
+  }
+}
+
+STATIC
+VOID
+AddRuntimeServicesRegion (
+  IN ARM_MEMORY_REGION_DESCRIPTOR *Desc
+)
+{
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+Desc->PhysicalBase,
+Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiRuntimeServicesData
+  );
+}
+
+STATIC
+VOID
+AddReservedMemoryRegion (
+  IN ARM_MEMORY_REGION_DESCRIPTOR *Desc
+  )
+{
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+Desc->PhysicalBase,
+Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiReservedMemoryType
+  );
+}
+
+/*++
+
+Routine Description:
+
+
+
+Arguments:
+
+  FileHandle  - Handle of the file being invoked.
+  PeiServices - Describes the list of possible PEI Services.
+
+Returns:
+
+  Status -  EFI_SUCCESS if the boot mode could be set
+
+--*/
+EFI_STATUS
+EFIAPI
+MemoryPeim (
+  IN EFI_PHYSICAL_ADDRESS  UefiMemoryBase,
+  IN UINT64UefiMemorySize
+  )
+{
+  ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;
+
+  // Get Virtual Memory Map from the Platform Library
+  ArmPlatformGetVirtualMemoryMap (&MemoryTable);
+
+  // Ensure PcdSystemMemorySize has been set
+  ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);
+
+  // FD without variable store
+  AddReservedMemoryRegion (&MemoryTable[0]);
+
+  // Variable store.
+  AddRuntimeServicesRegion (&MemoryTable[1]);
+
+  // Trusted Firmware region
+  AddReservedMemoryRegion (&MemoryTable[2]);
+
+  // Usable memory.
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+MemoryTable[3].PhysicalBase,
+MemoryTable[3].Length
+  );
+
+  AddReservedMemoryRegion (&MemoryTable[4]);
+
+  // Build Memory Allocation Hob
+  InitMmu (MemoryTable);
+
+  if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {
+// Optional feature that helps prevent EFI memory map fragmentation.
+BuildMemoryTypeInformationHob ();
+  }
+
+  return EFI_SUCCESS;
+}
diff --git 
a/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf 
b/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInit

[edk2] [PATCH v4 edk2-platforms 02/23] Silicon/Broadcom/Bcm283x: Add GpioLib

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h | 49 
+++
 Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h  | 33 
 Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c  | 89 

 Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf| 39 +
 4 files changed, 210 insertions(+)

diff --git a/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h 
b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h
new file mode 100644
index ..5fc43ddaa27b
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h
@@ -0,0 +1,49 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ **/
+
+#ifndef __BCM2836_GPIO_H__
+#define __BCM2836_GPIO_H__
+
+#define GPIO_BASE_ADDRESS  (BCM2836_SOC_REGISTERS + 0x0020)
+
+#define GPIO_GPFSEL0   (GPIO_BASE_ADDRESS + 0x00)
+#define GPIO_GPFSEL1   (GPIO_BASE_ADDRESS + 0x04)
+#define GPIO_GPFSEL2   (GPIO_BASE_ADDRESS + 0x08)
+#define GPIO_GPFSEL3   (GPIO_BASE_ADDRESS + 0x0C)
+#define GPIO_GPFSEL4   (GPIO_BASE_ADDRESS + 0x10)
+#define GPIO_GPFSEL5   (GPIO_BASE_ADDRESS + 0x14)
+
+#define GPIO_GPCLR0(GPIO_BASE_ADDRESS + 0x28)
+#define GPIO_GPCLR1(GPIO_BASE_ADDRESS + 0x2C)
+
+#define GPIO_GPSET0(GPIO_BASE_ADDRESS + 0x1C)
+#define GPIO_GPSET1(GPIO_BASE_ADDRESS + 0x20)
+
+#define GPIO_FSEL_INPUT0x0
+#define GPIO_FSEL_OUTPUT   0x1
+#define GPIO_FSEL_ALT0 0x4
+#define GPIO_FSEL_ALT1 0x5
+#define GPIO_FSEL_ALT2 0x6
+#define GPIO_FSEL_ALT3 0x7
+#define GPIO_FSEL_ALT4 0x3
+#define GPIO_FSEL_ALT5 0x2
+
+#define GPIO_FSEL_PINS_PER_REGISTER 10
+#define GPIO_FSEL_BITS_PER_PIN  3
+#define GPIO_FSEL_MASK  ((1 << GPIO_FSEL_BITS_PER_PIN) - 1)
+
+#define GPIO_PINS  54
+
+#endif /* __BCM2836_GPIO_H__ */
diff --git a/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h 
b/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h
new file mode 100644
index ..c3e1fc21bf8d
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h
@@ -0,0 +1,33 @@
+/** @file
+ *
+ *  GPIO manipulation.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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.
+ *
+ **/
+
+#ifndef __GPIO_LIB__
+#define __GPIO_LIB__
+
+#include 
+
+VOID
+GpioPinFuncSet (
+  IN  UINTN Pin,
+  IN  UINTN Function
+  );
+
+UINTN
+GpioPinFuncGet (
+  IN  UINTN Pin
+  );
+
+#endif /* __GPIO_LIB__ */
diff --git a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c 
b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c
new file mode 100644
index ..8cf560e4fcc5
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c
@@ -0,0 +1,89 @@
+/** @file
+ *
+ *  GPIO manipulation.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC
+VOID
+GpioFSELModify (
+  IN  UINTN RegIndex,
+  IN  UINT32 ModifyMask,
+  IN  UINT32 FunctionMask
+  )
+{
+  UINT32 Val;
+  EFI_PHYSICAL_ADDRESS Reg;
+
+  Reg = RegIndex * sizeof (UINT32) + GPIO_GPFSEL0;
+
+  ASSERT (Reg <= GPIO_GPFSEL5);
+  ASSERT ((~ModifyMask & FunctionMask) == 0);
+
+  Val = MmioRead32 (Reg);
+  Val &= ~ModifyMask;
+  Val |= FunctionMask;
+  MmioWrite32 (Reg, Val);
+}
+
+VOID
+GpioPinFuncSet (
+  IN  UINTN Pin,
+  IN  UINTN Function
+  )
+{
+  UINTN RegIndex;
+  UINTN SelIndex;
+  UINT32 ModifyMask;
+  UINT32 FunctionMask;
+
+

[edk2] [PATCH v4 edk2-platforms 01/23] Silicon/Broadcom/Bcm282x: Add interrupt driver

2019-01-29 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 

Reviewed-by: Ard Biesheuvel 
---
 Silicon/Broadcom/Bcm283x/Bcm283x.dec   |  23 ++
 Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c   | 367 

 Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.inf |  48 +++
 Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836.h|  72 
 4 files changed, 510 insertions(+)

diff --git a/Silicon/Broadcom/Bcm283x/Bcm283x.dec 
b/Silicon/Broadcom/Bcm283x/Bcm283x.dec
new file mode 100644
index ..d193da4c0e1e
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Bcm283x.dec
@@ -0,0 +1,23 @@
+## @file
+#
+#  Copyright (c) 2019, Pete Batard 
+#
+#  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.
+#
+##
+
+[Defines]
+  DEC_SPECIFICATION  = 0x0001001A
+  PACKAGE_NAME   = Bcm283xPkg
+  PACKAGE_GUID   = 900C0F44-1152-4FF9-B9C5-933E2918C831
+  PACKAGE_VERSION= 1.0
+
+[Includes]
+  Include
diff --git a/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c 
b/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c
new file mode 100644
index ..9058aa94ffb9
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c
@@ -0,0 +1,367 @@
+/** @file
+ *
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+//
+// This currently only implements support for the architected timer interrupts
+// on the per-CPU interrupt controllers.
+//
+#define NUM_IRQS(4)
+
+#ifdef MDE_CPU_AARCH64
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_AARCH64_IRQ
+#else
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_ARM_IRQ
+#endif
+
+STATIC CONST
+EFI_PHYSICAL_ADDRESS RegBase = FixedPcdGet32 (PcdInterruptBaseAddress);
+
+//
+// Notifications
+//
+STATIC EFI_EVENTmExitBootServicesEvent;
+STATIC HARDWARE_INTERRUPT_HANDLER   mRegisteredInterruptHandlers[NUM_IRQS];
+
+/**
+  Shutdown our hardware
+
+  DXE Core will disable interrupts and turn off the timer and disable 
interrupts
+  after all the event handlers have run.
+
+  @param[in]  Event   The Event that is being processed
+  @param[in]  Context Event Context
+**/
+STATIC
+VOID
+EFIAPI
+ExitBootServicesEvent (
+  IN EFI_EVENT  Event,
+  IN VOID   *Context
+  )
+{
+  // Disable all interrupts
+  MmioWrite32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 0);
+}
+
+/**
+  Enable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt enabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+EnableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT (FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioOr32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 1 << Source);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Disable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt disabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+DisableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT (FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioAnd32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, ~(1 << Source));
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Register Handler for the specified interrupt source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+  @param Handler  Callback for interrupt. NULL to unregister
+
+  @retval EFI_SUCCESS Source was updated to support Handler.
+  @retval E

[edk2] [PATCH v4 edk2-platforms 00/23] Platform/Raspberry: Add Raspberry Pi 3 support

2019-01-29 Thread Pete Batard
Changes applied to v4:

* Silicon/Broadcom/Include has been moved to Silicon/Broadcom/Bcm283x/Include.
  The [Packages] and [Includes] directives were also updated accordingly.
* Move the GpioLib function declarations into their own separate header.
* Add NOOPT to BUILD_TARGETS.
* Remove the no longer needed '-mcmodel=small' workaround from AcpiTables.

Changes not applied to v4:

* Ensure that all the ACPI tables _CID names, and the rest of the tables, are
  ACPI specs compliant, since we are constrained with regards to their usage
  for Microsoft Windows.


Preamble:

Because of its price point, ease of use and availability, the Raspberry Pi is
undeniably one of the most successful ARM platform in existence today. Its
widespread adoption therefore makes it a perfect fit as an EDK2 platform.

However, up until now, the Raspberry Pi hasn't been supported as a bona fide
platform in our repository. This series of patches remedies that by introducing
the Raspberry Pi 3 Model B and Model B+ as a viable EDK2 platform.

Notes regarding non-OSI content:

* Even though the ARM Trusted Firmware binary blobs are subject to a
  BSD-3-Clause licence, which may be compatible with the EDK2 one, we chose
  to follow the lead of other platforms that provide ATF binaries in non OSI.
  Ultimately, once there is a new dot release of ATF, we plan to remove these
  binaries and point to a dot release build configuartion.
* The Device Tree binaries (and source descriptors) are subject to a GPLv2
  license, as per the ones published by the Raspberry Pi Foundation.
* The Logo source code is under an EDK2 license, but the logo itself, which
  we obtained authorisation to use from the Raspberry Pi Foundation itself
  after detailing our planned usage, is subject to the trademark licensing
  terms put forward by the Foundation.

Additional Notes:

* Detailed instructions on how to build and test the platform firmware are
  included in the Readme.md found at the root of the platform.
* As detailed in the Readme, the resulting platform firmware has been
  successfully used to install and run Linux OSes, such as Ubuntu 18.10, as
  well as Windows 10 1809 (*full* UI version, not IoT).

Regards,

/Pete

Pete Batard (23):
  Silicon/Broadcom/Bcm282x: Add interrupt driver
  Silicon/Broadcom/Bcm283x: Add GpioLib
  Platform/Raspberry/Pi3: Add ACPI tables
  Platform/Raspberry/Pi3: Add reset and memory init libraries
  Platform/Raspberry/Pi3: Add platform library
  Platform/Raspberry/Pi3: Add RTC library
  Platform/Raspberry/Pi3: Add firmware driver
  Platform/Raspberry/Pi3: Add platform config driver
  Platform/Raspberry/Pi3: Add SMBIOS driver
  Platform/Raspberry/Pi3: Add display driver
  Platform/Raspberry/Pi3: Add console driver
  Platform/Raspberry/Pi3: Add NV storage driver
  Platform/Raspberry/Pi3: Add Device Tree driver
  Platform/Raspberry/Pi3: Add base MMC driver
  Platform/Raspberry/Pi3: Add Arasan MMC driver
  Platform/Raspberry/Pi3: Platform/Raspberry/Pi3: Add SD Host driver
  Platform/Raspberry/Pi3: Add platform boot manager and helper libraries
  Platform/Raspberry/Pi3: Add USB host driver
  Platform/Raspberry/Pi3: Add platform
  Platform/Raspberry/Pi3: Add platform readme
  Platform/Raspberry/Pi3 *NON-OSI*: Add ATF binaries
  Platform/Raspberry/Pi3 *NON-OSI*: Add Device Tree binaries
  Platform/Raspberry/Pi3 *NON-OSI*: Add logo driver

 .../Raspberry/Pi3/AcpiTables/AcpiTables.h |   82 +
 .../Raspberry/Pi3/AcpiTables/AcpiTables.inf   |   46 +
 Platform/Raspberry/Pi3/AcpiTables/Csrt.aslc   |  332 +++
 Platform/Raspberry/Pi3/AcpiTables/Dbg2.aslc   |   34 +
 Platform/Raspberry/Pi3/AcpiTables/Dsdt.asl|  511 +
 Platform/Raspberry/Pi3/AcpiTables/Fadt.aslc   |   52 +
 Platform/Raspberry/Pi3/AcpiTables/Gtdt.aslc   |   33 +
 Platform/Raspberry/Pi3/AcpiTables/Madt.aslc   |   62 +
 Platform/Raspberry/Pi3/AcpiTables/Pep.asl |   95 +
 Platform/Raspberry/Pi3/AcpiTables/Pep.c   |   84 +
 Platform/Raspberry/Pi3/AcpiTables/Pep.h   |  126 ++
 Platform/Raspberry/Pi3/AcpiTables/Rhpx.asl|  201 ++
 Platform/Raspberry/Pi3/AcpiTables/Sdhc.asl|  105 +
 Platform/Raspberry/Pi3/AcpiTables/Spcr.asl|   53 +
 Platform/Raspberry/Pi3/AcpiTables/Uart.asl|  158 ++
 Platform/Raspberry/Pi3/DeviceTree/License.txt |  340 +++
 .../Pi3/DeviceTree/bcm2710-rpi-3-b-plus.dtb   |  Bin 0 -> 25617 bytes
 .../Pi3/DeviceTree/bcm2710-rpi-3-b-plus.dts   | 1263 
 .../Pi3/DeviceTree/bcm2710-rpi-3-b.dtb|  Bin 0 -> 25354 bytes
 .../Pi3/DeviceTree/bcm2710-rpi-3-b.dts| 1259 +++
 .../ArasanMmcHostDxe/ArasanMmcHostDxe.c   |  723 +++
 .../ArasanMmcHostDxe/ArasanMmcHostDxe.h   |   50 +
 .../ArasanMmcHostDxe/ArasanMmcHostDxe.inf |   52 +
 .../Pi3/Drivers/ConfigDxe/ConfigDxe.c |  351 
 .../Pi3/Drivers/ConfigDxe/ConfigDxe.inf   |   78 +
 .../Drivers/ConfigDxe/ConfigDxeFormSetGuid.h  |   23 +
 .../Pi3/Drivers/ConfigDxe/ConfigDxeHii.uni|  100 +
 .../

Re: [edk2] [PATCH v3 edk2-platforms 03/23] Platform/Raspberry/Pi3: Add ACPI tables

2019-01-29 Thread Pete Batard

Hi Ard, thanks for the reviews,

On 2019.01.28 13:24, Ard Biesheuvel wrote:

A couple of general remarks on these tables.

- AFAICT (but is is difficult to tell from the binary dumps), these
are based on ACPI 5.0, which predates the introduction of ARM support
into the spec. That essentially makes them Windows-only, and not spec
based, which is unfortunate. However, since this SoC does not have an
interrupt controller permitted by ACPI, that actually does not matter
a lot in practice.


Publicly, these tables are only meant to boot Windows and we got them 
from Microsoft. So they are not exactly meant to be ACPI compliant (for 
instance, AFAIK, there's no definition of ACPI that includes the RPI 
interrupt controller).


As such, we really don't want to touch these because we have run into 
nightmarish situation where Windows wouldn't boot at all after we tried 
to alter them.


In other words, if we are to try to change much in here, we will 
probably need to have Microsoft's involvement...



- _CID fields should be properly formatted ACPI or PNP handles, you
can't put arbitrary strings in there (like BCMAUXSPI or VC4)


The Microsoft Windows drivers expect those exact IDs, so that's not 
something we can alter either. Once again, these ACPI tables were not 
designed to be ACPI compliant, and making them so that late in the game 
is going to break a lot of things downstream, so we don't believe that 
we can do that at this stage.



I will apply your other request for ACPI (drop the "-mcmodel=tiny" 
workaround in [BuildOptions]), as well as the changes you requested for 
1/2/19 and, if you are okay with having our hands tied with regards to 
ACPI compliance, submit a v4 later on today.


Regards,

/Pete
___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


[edk2] [PATCH v3 edk2-platforms 2/23] Platform/Raspberry/Pi3 *NON-OSI*: Add Device Tree binaries

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/DeviceTree/License.txt  |  340 ++
 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b-plus.dtb |  Bin 0 -> 25617 
bytes
 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b-plus.dts | 1263 

 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b.dtb  |  Bin 0 -> 25354 
bytes
 Platform/Raspberry/Pi3/DeviceTree/bcm2710-rpi-3-b.dts  | 1259 
+++
 5 files changed, 2862 insertions(+)

diff --git a/Platform/Raspberry/Pi3/DeviceTree/License.txt 
b/Platform/Raspberry/Pi3/DeviceTree/License.txt
new file mode 100644
index ..1603937dad82
--- /dev/null
+++ b/Platform/Raspberry/Pi3/DeviceTree/License.txt
@@ -0,0 +1,340 @@
+GNU GENERAL PUBLIC LICENSE
+   Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy a

[edk2] [PATCH v3 edk2-platforms 21/23] Platform/Raspberry/Pi3 *NON-OSI*: Add ATF binaries

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/TrustedFirmware/License.txt |  26 
 Platform/Raspberry/Pi3/TrustedFirmware/README.md   |  42 
 Platform/Raspberry/Pi3/TrustedFirmware/bl1.bin | Bin 0 -> 18801 bytes
 Platform/Raspberry/Pi3/TrustedFirmware/fip.bin | Bin 0 -> 41714 bytes
 4 files changed, 68 insertions(+)

diff --git a/Platform/Raspberry/Pi3/TrustedFirmware/License.txt 
b/Platform/Raspberry/Pi3/TrustedFirmware/License.txt
new file mode 100644
index ..b98dc643227e
--- /dev/null
+++ b/Platform/Raspberry/Pi3/TrustedFirmware/License.txt
@@ -0,0 +1,26 @@
+Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without 
modification,
+are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice, 
this
+  list of conditions and the following disclaimer in the documentation and/or
+  other materials provided with the distribution.
+
+* Neither the name of ARM nor the names of its contributors may be used to
+  endorse or promote products derived from this software without specific prior
+  written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Platform/Raspberry/Pi3/TrustedFirmware/README.md 
b/Platform/Raspberry/Pi3/TrustedFirmware/README.md
new file mode 100644
index ..862933f6f2ba
--- /dev/null
+++ b/Platform/Raspberry/Pi3/TrustedFirmware/README.md
@@ -0,0 +1,42 @@
+ARM Trusted Firmware for Raspberry Pi 3
+===
+
+The `bl1` and `fip` ATF binaries, found in this directory, were built from
+the [latest ATF](https://github.com/ARM-software/arm-trusted-firmware)
+(commit c3859557) using Linaro's GCC 5.5 compiler with:
+
+```
+export 
CROSS_COMPILE=/usr/src/gcc-linaro-5.5.0-2017.10-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
+make PLAT=rpi3 PRELOADED_BL33_BASE=0x3 RPI3_PRELOADED_DTB_BASE=0x1 
SUPPORT_VFP=1 RPI3_USE_UEFI_MAP=1 fip all
+```
+
+This results in the following memory mapping:
+
+```
+0x +-+
+   |   ROM   | BL1
+0x0001 +-+
+   |   DTB   | (Loaded by the VideoCore)
+0x0002 +-+
+   |   FIP   |
+0x0003 +-+
+   | |
+   |  UEFI PAYLOAD   |
+   | |
+0x0020 +-+
+   |   Secure SRAM   | BL2, BL31
+0x0030 +-+
+   |   Secure DRAM   | BL32 (Secure payload)
+0x0040 +-+
+   | |
+   | |
+   | Non-secure DRAM | BL33
+   | |
+   | |
+0x0100 +-+
+   | |
+   |   ...   |
+   | |
+0x3F00 +-+
+   |   I/O   |
+```
diff --git a/Platform/Raspberry/Pi3/TrustedFirmware/bl1.bin 
b/Platform/Raspberry/Pi3/TrustedFirmware/bl1.bin
new file mode 100644
index 
..e25138828d0a4ddb24772abb1a60eefc334666b5
GIT binary patch
literal 18801
zcmeHud2|%#mG7;pmedFVtpy|tJ-S*z4DF582<)+{TLO%YF^IMyw%@2*ivhC;Enpe2
zRTADp&Ya2NoOkF$@l}1x-M+hhw@Q@U820D96C>N0$f-gpvNVO-6r%k^_LFjhV53f>
zzYYB^p(#wZhayBm(40?m)v%ZRF-jkU{?@RJdpUDVq_0@Qvl>>=KM8BSMAN6D9c2X^
zv}!gTC9-IZXdThl_aE5oz6A)Ol0}J=O^M4yanPmrUEBE6z%_}qi$f`XQKFzu
zlVZ_?G$rnX41{DgYzORyWMj?6p-TMP#*g+gylWdj3`8X%sIOlv1lmB)R$NV@Mf2q+
zY4d>}JWrcSYDNxGR%4SA-?^DI?>kETG0>;<^-cF@D)C*r!0X4PiJ}^Q9sQugsR%T^
zW2zF8L8m(ZKusahN`})&mx1t%~~5>0PQR^krEJ6wr3iAo6aZSmWc_!Ag6
z`56X&xUWmf4DJH$r=?NBrxUaj_-e#D|7a!d7m4LPR23=2^S_NbBGxi5Ce4?hcK6f$
z3TXVu>I#)XZeBsnU^%~vzTYcJS`*qLzY-q@C2Lj%?d)EP7gLhfKy+cZh2moYa~$X{
zx3%XUwhAHXdf#-#Z|vK6?76XLKQ{Io`^9@z&g

[edk2] [PATCH v3 edk2-platforms 20/23] Platform/Raspberry/Pi3: Add platform readme

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Readme.md | 259 
 Readme.md|   3 +
 2 files changed, 262 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Readme.md b/Platform/Raspberry/Pi3/Readme.md
new file mode 100644
index ..7fb59ccdc321
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Readme.md
@@ -0,0 +1,259 @@
+Raspberry Pi 3 EDK2 Platform Support
+
+
+# Summary
+
+This is a port of 64-bit Tiano Core UEFI firmware for the Raspberry Pi 3/3B+ 
platforms,
+based on [Ard Bisheuvel's 
64-bit](http://www.workofard.com/2017/02/uefi-on-the-pi/)
+and [Microsoft's 
32-bit](https://github.com/ms-iot/RPi-UEFI/tree/ms-iot/Pi3BoardPkg)
+implementations, as maintained by [Andrei 
Warkentin](https://github.com/andreiw/RaspberryPiPkg).
+
+This is meant as a generally useful 64-bit ATF + UEFI implementation for the 
Raspberry
+Pi 3/3B+ which should be good enough for most kind of UEFI development, as 
well as for
+running consummer Operating Systems in such as Linux or Windows.
+
+Raspberry Pi is a trademark of the [Raspberry Pi 
Foundation](http://www.raspberrypi.org).
+
+# Status
+
+This firmware, that has been validated to compile against the current
+[edk2](https://github.com/tianocore/edk2)/[edk2-platforms](https://github.com/tianocore/edk2-platforms),
+should be able to boot Linux (SUSE, Ubuntu), NetBSD, FreeBSD as well as 
Windows 10 ARM64
+(full GUI version).
+
+It also provides support for ATF ([Arm Trusted 
Platform](https://github.com/ARM-software/arm-trusted-firmware)).
+
+HDMI and the mini-UART serial port can be used for output devices, with 
mirrored output.
+USB keyboards and the mini-UART serial port can be used as input.
+
+The boot order is currently hardcoded, first to the USB ports and then to the 
uSD card.
+If there no bootable media media is found, the UEFI Shell is launched.
+Esc enters platform setup. F1 boots the UEFI Shell.
+
+# Building
+
+(These instructions were validated against the latest edk2 / edk2-platforms /
+edk2-non-osi as of 2019.01.27, on a Debian 9.6 x64 system).
+
+You may need to install the relevant compilation tools. Especially you should 
have the
+ACPI Source Language (ASL) compiler, `nasm` as well as a native compiler 
installed.
+On a Debian system, you can get these prerequisites installed with:
+```
+sudo apt-get install build-essential acpica-tools nasm uuid-dev
+```
+
+**IMPORTANT:** We recommend the use of the Linaro GCC for compilation instead 
of
+your system's native ARM64 GCC cross compiler.
+
+You can then build the firmware as follows:
+
+* Standalone instructions
+
+```
+mkdir ~/workspace
+cd ~/workspace
+git clone https://github.com/tianocore/edk2.git
+# The following is only needed once, after you cloned edk2
+make -C edk2/BaseTools
+git clone https://github.com/tianocore/edk2-platforms.git
+git clone https://github.com/tianocore/edk2-non-osi.git
+wget 
https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/aarch64-linux-gnu/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz
+tar -xJvf gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz
+# If you have multiple AARCH64 toolchains, make sure the above one comes first 
in your path
+export PATH=$PWD/gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu/bin:$PATH
+export GCC5_AARCH64_PREFIX=aarch64-linux-gnu-
+export WORKSPACE=$PWD
+export 
PACKAGES_PATH=$WORKSPACE/edk2:$WORKSPACE/edk2-platforms:$WORKSPACE/edk2-non-osi
+. edk2/edksetup.sh
+build -a AARCH64 -t GCC5 -p edk2-platforms/Platform/Raspberry/Pi3/RPi3.dsc 
-DBUILD_EPOCH=`date +%s` -b RELEASE
+```
+
+# Booting the firmware
+
+1. Format a uSD card as FAT32
+2. Copy the generated `RPI_EFI.fd` firmware onto the partition
+3. Download and copy the following files from 
https://github.com/raspberrypi/firmware/tree/master/boot
+  - `bootcode.bin`
+  - `fixup.dat`
+  - `start.elf`
+4. Create a `config.txt` with the following content:
+  ```
+  arm_control=0x200
+  enable_uart=1
+  armstub=RPI_EFI.fd
+  disable_commandline_tags=1
+  ```
+5. Insert the uSD card and power up the Pi.
+
+Note that if you have a model 3+ or a model 3 where you enabled USB boot 
through OTP
+(see 
[here](https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/msd.md))
+you may also be able to boot from a FAT32 USB driver rather than uSD.
+
+# Notes
+
+## ARM Trusted Firmware (ATF)
+
+The ATF binaries being used were compiled from the ATF mainline.
+
+For more details on the ATF compilation, see the [README](./Binary/README.md) 
in the `Binary/` directory.
+
+## Custom Device Tree
+
+The default Device Tree included in the firmware is the one for a Raspberry Pi 
3 Model B (not B+).
+If you want to use a different Device Tree, to boot a Pi 3 Model B+ for 
instance (for which a
+DTB is also provided under `DeviceTree/`), you should copy the relevant `.dtb` 
into the root of
+the

[edk2] [PATCH v3 edk2-platforms 19/23] Platform/Raspberry/Pi3: Add platform

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/RPi3.dec |  60 ++
 Platform/Raspberry/Pi3/RPi3.dsc | 637 
 Platform/Raspberry/Pi3/RPi3.fdf | 449 ++
 3 files changed, 1146 insertions(+)

diff --git a/Platform/Raspberry/Pi3/RPi3.dec b/Platform/Raspberry/Pi3/RPi3.dec
new file mode 100644
index ..d65a4adc8eeb
--- /dev/null
+++ b/Platform/Raspberry/Pi3/RPi3.dec
@@ -0,0 +1,60 @@
+## @file
+#
+#  Copyright (c) 2016, Linaro, Ltd. All rights reserved.
+#  Copyright (c) 2017-2018, Andrei Warkentin 
+#
+#  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.
+#
+##
+
+[Defines]
+  DEC_SPECIFICATION  = 0x0001001A
+  PACKAGE_NAME   = RPi3Pkg
+  PACKAGE_GUID   = DFA0CA8B-F3AC-4607-96AC-46FA04B84DCC
+  PACKAGE_VERSION= 1.0
+
+[Includes]
+  Include
+  Silicon/Broadcom/Include
+
+[Protocols]
+  gRaspberryPiFirmwareProtocolGuid = { 0x0ACA9535, 0x7AD0, 0x4286, { 0xB0, 
0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiConfigAppliedProtocolGuid = { 0x0ACA, 0x7AD0, 0x4286, { 
0xB0, 0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiMmcHostProtocolGuid = { 0x3e591c00, 0x9e4a, 0x11df, {0x92, 0x44, 
0x00, 0x02, 0xA5, 0xF5, 0xF5, 0x1B } }
+  gExtendedTextOutputProtocolGuid = { 0x387477ff, 0xffc7, 0xffd2, {0x8e, 0x39, 
0x0, 0xff, 0xc9, 0x69, 0x72, 0x3b } }
+
+[Guids]
+  gRaspberryPiTokenSpaceGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+  gRaspberryPiFdtFileGuid = {0xDF5DA223, 0x1D27, 0x47C3, { 0x8D, 0x1B, 0x9A, 
0x41, 0xB5, 0x5A, 0x18, 0xBC}}
+  gRaspberryPiEventResetGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB4, 0xB4, 0xE4, 0xD4, 0xB4}}
+  gConfigDxeFormSetGuid = {0xCD7CC258, 0x31DB, 0x22E6, {0x9F, 0x22, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+
+[PcdsFixedAtBuild.common]
+  gRaspberryPiTokenSpaceGuid.PcdFdtBaseAddress|0x1|UINT32|0x0001
+  gRaspberryPiTokenSpaceGuid.PcdFirmwareBlockSize|0x0|UINT32|0x0002
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogBase|0x0|UINT32|0x0003
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogSize|0x0|UINT32|0x0004
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageVariableBase|0x0|UINT32|0x0005
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwSpareBase|0x0|UINT32|0x0006
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwWorkingBase|0x0|UINT32|0x0007
+  gRaspberryPiTokenSpaceGuid.PcdBootEpochSeconds|0x0|UINT64|0x0008
+
+[PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx]
+  gRaspberryPiTokenSpaceGuid.PcdCpuClock|0|UINT32|0x000d
+  gRaspberryPiTokenSpaceGuid.PcdSdIsArasan|0|UINT32|0x000e
+  gRaspberryPiTokenSpaceGuid.PcdMmcForce1Bit|0|UINT32|0x000f
+  gRaspberryPiTokenSpaceGuid.PcdMmcForceDefaultSpeed|0|UINT32|0x0010
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdDefaultSpeedMHz|0|UINT32|0x0011
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdHighSpeedMHz|0|UINT32|0x0012
+  gRaspberryPiTokenSpaceGuid.PcdMmcDisableMulti|0|UINT32|0x0013
+  gRaspberryPiTokenSpaceGuid.PcdDebugEnableJTAG|0|UINT32|0x0014
+  gRaspberryPiTokenSpaceGuid.PcdDebugShowUEFIExit|0|UINT32|0x0015
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableVModes|0|UINT32|0x0017
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableSShot|0|UINT32|0x0018
diff --git a/Platform/Raspberry/Pi3/RPi3.dsc b/Platform/Raspberry/Pi3/RPi3.dsc
new file mode 100644
index ..effe9d243209
--- /dev/null
+++ b/Platform/Raspberry/Pi3/RPi3.dsc
@@ -0,0 +1,637 @@
+# @file
+#
+#  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
+#  Copyright (c) 2014, Linaro Limited. All rights reserved.
+#  Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
+#  Copyright (c) 2017 - 2018, Andrei Warkentin 
+#
+#  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.
+#
+##
+
+
+#
+# Defines Section - statements that will be processed to create a Makefile.
+#
+
+[Defines]
+  PLATFORM_NAME  = RPi3
+  PLATFORM_GUID 

[edk2] [PATCH v3 edk2-platforms 18/23] Platform/Raspberry/Pi3: Add USB host driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c  |  226 +++
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DriverBinding.c  |  275 
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.c   | 1637 

 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.h   |  165 ++
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwUsbHostDxe.inf |   52 +
 Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/DwcHw.h  |  791 ++
 Platform/Raspberry/Pi3/Include/Protocol/DwUsb.h  |   53 +
 7 files changed, 3199 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c
new file mode 100644
index ..0f4db4e21204
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/DwUsbHostDxe/ComponentName.c
@@ -0,0 +1,226 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrey Warkentin 
+ *
+ *
+ *  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 "DwUsbHostDxe.h"
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  );
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL *This,
+  IN  EFI_HANDLE  ControllerHandle,
+  IN  EFI_HANDLE  ChildHandle,
+  IN  CHAR8   *Language,
+  OUT CHAR16  **ControllerName
+  );
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
+  ComponentNameGetDriverName,
+  ComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)ComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)ComponentNameGetControllerName,
+  "en"
+};
+
+
+STATIC EFI_UNICODE_STRING_TABLE mDriverName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi USB Host Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mDeviceName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi USB Host"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  E

[edk2] [PATCH v3 edk2-platforms 17/23] Platform/Raspberry/Pi3: Add platform boot manager and helper libraries

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c 
  | 793 
 Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.h 
  |  60 ++
 
Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
 |  90 +++
 Platform/Raspberry/Pi3/Library/PlatformUiAppLib/PlatformUiAppLib.c 
  | 120 +++
 Platform/Raspberry/Pi3/Library/PlatformUiAppLib/PlatformUiAppLib.inf   
  |  34 +
 5 files changed, 1097 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c 
b/Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c
new file mode 100644
index ..9bbe0db64950
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Library/PlatformBootManagerLib/PlatformBm.c
@@ -0,0 +1,793 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Pete Batard 
+ *  Copyright (c) 2017-2018, Andrei Warkentin 
+ *  Copyright (c) 2016, Linaro Ltd. All rights reserved.
+ *  Copyright (c) 2015-2016, Red Hat, Inc.
+ *  Copyright (c) 2014, ARM Ltd. All rights reserved.
+ *  Copyright (c) 2004-2016, Intel Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "PlatformBm.h"
+
+#define BOOT_PROMPT L"ESC (setup), F1 (shell), ENTER (boot)"
+
+#define DP_NODE_LEN(Type) { (UINT8)sizeof (Type), (UINT8)(sizeof (Type) >> 8) }
+
+#pragma pack (1)
+typedef struct {
+  VENDOR_DEVICE_PATH SerialDxe;
+  UART_DEVICE_PATH   Uart;
+  VENDOR_DEFINED_DEVICE_PATH TermType;
+  EFI_DEVICE_PATH_PROTOCOL   End;
+} PLATFORM_SERIAL_CONSOLE;
+#pragma pack ()
+
+typedef struct {
+  VENDOR_DEVICE_PATH Custom;
+  USB_DEVICE_PATHHub;
+  USB_DEVICE_PATHDev;
+  EFI_DEVICE_PATH_PROTOCOL   EndDevicePath;
+} PLATFORM_USB_DEV;
+
+typedef struct {
+  VENDOR_DEVICE_PATH Custom;
+  EFI_DEVICE_PATH_PROTOCOL   EndDevicePath;
+} PLATFORM_SD_DEV;
+
+#define ARASAN_MMC_DXE_FILE_GUID  \
+  { 0x100c2cfa, 0xb586, 0x4198, { 0x9b, 0x4c, 0x16, 0x83, 0xd1, 0x95, 0xb1, 
0xda } }
+
+#define SDHOST_MMC_DXE_FILE_GUID  \
+  { 0x58abd787, 0xf64d, 0x4ca2, { 0xa0, 0x34, 0xb9, 0xac, 0x2d, 0x5a, 0xd0, 
0xcf } }
+
+#define SERIAL_DXE_FILE_GUID  \
+  { 0xD3987D4B, 0x971A, 0x435F, { 0x8C, 0xAF, 0x49, 0x67, 0xEB, 0x62, 0x72, 
0x41 } }
+
+STATIC PLATFORM_SD_DEV mArasan = {
+  //
+  // VENDOR_DEVICE_PATH ArasanMMCHostDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+ARASAN_MMC_DXE_FILE_GUID
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+STATIC PLATFORM_SD_DEV mSDHost = {
+  //
+  // VENDOR_DEVICE_PATH SdHostDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+SDHOST_MMC_DXE_FILE_GUID
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+STATIC PLATFORM_SERIAL_CONSOLE mSerialConsole = {
+  //
+  // VENDOR_DEVICE_PATH SerialDxe
+  //
+  {
+{ HARDWARE_DEVICE_PATH, HW_VENDOR_DP, DP_NODE_LEN (VENDOR_DEVICE_PATH) },
+SERIAL_DXE_FILE_GUID
+  },
+
+  //
+  // UART_DEVICE_PATH Uart
+  //
+  {
+{ MESSAGING_DEVICE_PATH, MSG_UART_DP, DP_NODE_LEN (UART_DEVICE_PATH) },
+0,  // Reserved
+FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
+FixedPcdGet8 (PcdUartDefaultDataBits),  // DataBits
+FixedPcdGet8 (PcdUartDefaultParity),// Parity
+FixedPcdGet8 (PcdUartDefaultStopBits)   // StopBits
+  },
+
+  //
+  // VENDOR_DEFINED_DEVICE_PATH TermType
+  //
+  {
+{
+  MESSAGING_DEVICE_PATH, MSG_VENDOR_DP,
+  DP_NODE_LEN (VENDOR_DEFINED_DEVICE_PATH)
+}
+//
+// Guid to be filled in dynamically
+//
+  },
+
+  //
+  // EFI_DEVICE_PATH_PROTOCOL End
+  //
+  {
+END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,
+DP_NODE_LEN (EFI_DEVICE_PATH_PROTOCOL)
+  }
+};
+
+
+#pragma pack (1)
+typedef struct {
+  USB_CLASS_DEVICE_PATHKeyboard;
+  EFI_DEVICE_PATH_PROTOCOL End;
+} PLATFORM_USB_KEYBOARD;
+#pragma pack ()
+
+STATIC PLATFORM_USB_KEYBOARD mUsbKeyboard = {
+  //

[edk2] [PATCH v3 edk2-platforms 16/23] Platform/Raspberry/Pi3: Add SD Host driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c  | 787 

 Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.inf|  54 ++
 Silicon/Broadcom/Include/IndustryStandard/Bcm2836SdHost.h |  92 +++
 3 files changed, 933 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c 
b/Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c
new file mode 100644
index ..3bf789f96b27
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/SdHostDxe/SdHostDxe.c
@@ -0,0 +1,787 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#define SDHOST_BLOCK_BYTE_LENGTH512
+
+// Driver Timing Parameters
+#define CMD_STALL_AFTER_POLL_US 1
+#define CMD_MIN_POLL_TOTAL_TIME_US  10 // 100ms
+#define CMD_MAX_POLL_COUNT  (CMD_MIN_POLL_TOTAL_TIME_US / 
CMD_STALL_AFTER_POLL_US)
+#define CMD_MAX_RETRY_COUNT 3
+#define CMD_STALL_AFTER_RETRY_US20 // 20us
+#define FIFO_MAX_POLL_COUNT 100
+#define STALL_TO_STABILIZE_US   1 // 10ms
+
+#define IDENT_MODE_SD_CLOCK_FREQ_HZ 40 // 400KHz
+
+// Macros adopted from MmcDxe internal header
+#define SDHOST_R0_READY_FOR_DATABIT8
+#define SDHOST_R0_CURRENTSTATE(Response)((Response >> 9) & 0xF)
+
+#define DEBUG_MMCHOST_SD   DEBUG_VERBOSE
+#define DEBUG_MMCHOST_SD_INFO  DEBUG_INFO
+#define DEBUG_MMCHOST_SD_ERROR DEBUG_ERROR
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL   *mFwProtocol;
+
+// Per Physical Layer Simplified Specs
+#ifndef NDEBUG
+STATIC CONST CHAR8* mStrSdState[] = { "idle", "ready", "ident", "stby",
+  "tran", "data", "rcv", "prg", "dis",
+  "ina" };
+STATIC CONST CHAR8 *mFsmState[] = { "identmode", "datamode", "readdata",
+"writedata", "readwait", "readcrc",
+"writecrc", "writewait1", "powerdown",
+"powerup", "writestart1", "writestart2",
+"genpulses", "writewait2", "?",
+"startpowdown" };
+#endif /* NDEBUG */
+STATIC UINT32 mLastGoodCmd = MMC_GET_INDX (MMC_CMD0);
+
+STATIC inline BOOLEAN
+IsAppCmd (
+  VOID
+  )
+{
+  return mLastGoodCmd == MMC_CMD55;
+}
+
+STATIC BOOLEAN
+IsBusyCmd (
+  IN  UINT32 MmcCmd
+  )
+{
+  if (IsAppCmd ()) {
+return FALSE;
+  }
+
+  return MmcCmd == MMC_CMD7 || MmcCmd == MMC_CMD12;
+}
+
+STATIC BOOLEAN
+IsWriteCmd (
+  IN  UINT32 MmcCmd
+  )
+{
+  if (IsAppCmd ()) {
+return FALSE;
+  }
+
+  return MmcCmd == MMC_CMD24 || MmcCmd == MMC_CMD25;
+}
+
+STATIC BOOLEAN
+IsReadCmd (
+  IN  UINT32 MmcCmd,
+  IN  UINT32 Argument
+  )
+{
+  if (MmcCmd == MMC_CMD8 && !IsAppCmd ()) {
+if (Argument == CMD8_MMC_ARG) {
+  DEBUG ((DEBUG_MMCHOST_SD, "Sending MMC CMD8 variant\n"));
+  return TRUE;
+} else {
+  ASSERT (Argument == CMD8_SD_ARG);
+  DEBUG ((DEBUG_MMCHOST_SD, "Sending SD CMD8 variant\n"));
+  return FALSE;
+}
+  }
+
+  return
+(MmcCmd == MMC_CMD6 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD17 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD18 && !IsAppCmd ()) ||
+(MmcCmd == MMC_CMD13 && IsAppCmd ()) ||
+(MmcCmd == MMC_ACMD22 && IsAppCmd ()) ||
+(MmcCmd == MMC_ACMD51 && IsAppCmd ());
+}
+
+STATIC VOID
+SdHostDumpRegisters (
+  VOID
+  )
+{
+  DEBUG ((DEBUG_MMCHOST_SD, "SdHost: Registers Dump:\n"));
+  DEBUG ((DEBUG_MMCHOST_SD, "  CMD:  0x%8.8X\n", MmioRead32 (SDHOST_CMD)));
+  DEBUG ((DEBUG_MMCHOST_SD, "  ARG:  0x%8.8X\n", MmioRead32 (SDHOST_ARG)));
+  DEBUG ((DEBUG_MMCHOST_SD, "  TOUT: 0x%8.8X\n", MmioRead32 (SDHOST_TOUT)));
+  DEBUG ((DEBUG_MMCHOST_SD, "  CDIV

[edk2] [PATCH v3 edk2-platforms 15/23] Platform/Raspberry/Pi3: Add Arasan MMC driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c   | 723 

 Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.h   |  50 ++
 Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.inf |  51 ++
 Silicon/Broadcom/Include/IndustryStandard/Bcm2836Sdio.h  | 199 
++
 4 files changed, 1023 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c 
b/Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
new file mode 100644
index ..828b40f82a5f
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/ArasanMmcHostDxe/ArasanMmcHostDxe.c
@@ -0,0 +1,723 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 "ArasanMmcHostDxe.h"
+
+#define DEBUG_MMCHOST_SD DEBUG_VERBOSE
+
+BOOLEAN PreviousIsCardPresent = FALSE;
+UINT32 LastExecutedCommand = (UINT32) -1;
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+/**
+   These SD commands are optional, according to the SD Spec
+**/
+BOOLEAN
+IgnoreCommand (
+  UINT32 Command
+  )
+{
+  switch (Command) {
+  case MMC_CMD20:
+return TRUE;
+  default:
+return FALSE;
+  }
+}
+
+/**
+   Translates a generic SD command into the format used by the Arasan SD Host 
Controller
+**/
+UINT32
+TranslateCommand (
+  UINT32 Command,
+  UINT32 Argument
+  )
+{
+  UINT32 Translation = 0x;
+
+  if (LastExecutedCommand == CMD55) {
+switch (Command) {
+case MMC_CMD6:
+  Translation = ACMD6;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD6\n"));
+  break;
+case MMC_ACMD22:
+  Translation = ACMD22;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD22\n"));
+  break;
+case MMC_ACMD41:
+  Translation = ACMD41;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD41\n"));
+  break;
+case MMC_ACMD51:
+  Translation = ACMD51;
+  DEBUG ((DEBUG_MMCHOST_SD, "ACMD51\n"));
+  break;
+default:
+  DEBUG ((DEBUG_ERROR, "ArasanMMCHost: TranslateCommand(): Unrecognized 
App command: %d\n", Command));
+}
+  } else {
+switch (Command) {
+case MMC_CMD0:
+  Translation = CMD0;
+  break;
+case MMC_CMD1:
+  Translation = CMD1;
+  break;
+case MMC_CMD2:
+  Translation = CMD2;
+  break;
+case MMC_CMD3:
+  Translation = CMD3;
+  break;
+case MMC_CMD5:
+  Translation = CMD5;
+  break;
+case MMC_CMD6:
+  Translation = CMD6;
+  break;
+case MMC_CMD7:
+  Translation = CMD7;
+  break;
+case MMC_CMD8: {
+  if (Argument == CMD8_SD_ARG) {
+Translation = CMD8_SD;
+DEBUG ((DEBUG_MMCHOST_SD, "Sending SD CMD8 variant\n"));
+  } else {
+ASSERT (Argument == CMD8_MMC_ARG);
+Translation = CMD8_MMC;
+DEBUG ((DEBUG_MMCHOST_SD, "Sending MMC CMD8 variant\n"));
+  }
+  break;
+}
+case MMC_CMD9:
+  Translation = CMD9;
+  break;
+case MMC_CMD11:
+  Translation = CMD11;
+  break;
+case MMC_CMD12:
+  Translation = CMD12;
+  break;
+case MMC_CMD13:
+  Translation = CMD13;
+  break;
+case MMC_CMD16:
+  Translation = CMD16;
+  break;
+case MMC_CMD17:
+  Translation = CMD17;
+  break;
+case MMC_CMD18:
+  Translation = CMD18;
+  break;
+case MMC_CMD23:
+  Translation = CMD23;
+  break;
+case MMC_CMD24:
+  Translation = CMD24;
+  break;
+case MMC_CMD25:
+  Translation = CMD25;
+  break;
+case MMC_CMD55:
+  Translation = CMD55;
+  break;
+default:
+  DEBUG ((DEBUG_ERROR, "ArasanMMCHost: TranslateCommand(): Unrecognized 
Command: %d\n", Command));
+}
+  }
+
+  return Translation;
+}
+
+/**
+   Repeatedly polls a register until its value becomes correct, or until 
MAX_RETRY_COUNT polls is reached
+**/
+EFI_STATUS
+PollRegisterWithMask (
+  IN UINTN Register,
+  IN UINTN Mask,
+  IN UINTN ExpectedValue
+  )
+{
+  UINTN RetryCount = 0;
+
+  while (RetryCount < MAX_RETRY_COUNT) {
+if ((MmioRead32 (Register) & Mask) != ExpectedValue) {
+  RetryCount++;
+  gBS->Stall (STALL_AFTER_RETRY_US);
+} else {
+  break;
+}
+  }
+
+  if (RetryCount == MAX_RETRY_COUNT) {
+return EFI_TIMEOUT;
+  }
+
+  return EFI_SUCCESS;
+}
+
+STATIC
+E

[edk2] [PATCH v3 edk2-platforms 14/23] Platform/Raspberry/Pi3: Add base MMC driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c | 163 
 Platform/Raspberry/Pi3/Drivers/MmcDxe/Diagnostics.c   | 263 ++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/Mmc.c   | 460 +
 Platform/Raspberry/Pi3/Drivers/MmcDxe/Mmc.h   | 533 +++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcBlockIo.c| 469 ++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcDebug.c  | 170 
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcDxe.inf  |  58 ++
 Platform/Raspberry/Pi3/Drivers/MmcDxe/MmcIdentification.c | 980 

 Platform/Raspberry/Pi3/Include/Protocol/RpiMmcHost.h  | 206 
 9 files changed, 3302 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c
new file mode 100644
index ..034da778cae2
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/MmcDxe/ComponentName.c
@@ -0,0 +1,163 @@
+/** @file
+ *
+ *  Component Name Protocol implementation for the MMC DXE driver
+ *
+ *  Copyright (c) 2011, ARM Limited. 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 "Mmc.h"
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL  gMmcComponentName = 
{
+  MmcGetDriverName,
+  MmcGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gMmcComponentName2 
= {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)MmcGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)MmcGetControllerName,
+  "en"
+};
+
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE
+mMmcDriverNameTable[] = {
+  {"eng;en", L"MMC/SD Card Interface Driver"},
+  {NULL,  NULL}
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+  @param  Language  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+  @param  DriverNameA pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+EFI_STATUS
+EFIAPI
+MmcGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+   Language,
+   This->SupportedLanguages,
+   mMmcDriverNameTable,
+   DriverName,
+   (BOOLEAN)(This == &gMmcComponentName)
+ );
+}
+
+/**
+  Retrieves a Unicode string that is the user readable name of the controller
+  that is being managed by a driver.
+
+  This function retrieves the user readable name of the controller specified by

[edk2] [PATCH v3 edk2-platforms 13/23] Platform/Raspberry/Pi3: Add Device Tree driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c   | 364 
 Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.inf |  53 +++
 2 files changed, 417 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c 
b/Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c
new file mode 100644
index ..eb5698cb505b
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/FdtDxe/FdtDxe.c
@@ -0,0 +1,364 @@
+/** @file
+ *
+ *  Copyright (c) 2017, Andrey Warkentin 
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+
+STATIC VOID *mFdtImage;
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL   *mFwProtocol;
+
+STATIC
+VOID
+UpdateMacAddress (
+  VOID
+  )
+{
+  INTN  Node;
+  INTN  Retval;
+  EFI_STATUSStatus;
+  UINT8 MacAddress[6];
+
+  //
+  // Locate the node that the 'ethernet' alias refers to
+  //
+  Node = fdt_path_offset (mFdtImage, "ethernet");
+  if (Node < 0) {
+DEBUG ((DEBUG_ERROR, "%a: failed to locate 'ethernet' alias\n", 
__FUNCTION__));
+return;
+  }
+
+  //
+  // Get the MAC address from the firmware
+  //
+  Status = mFwProtocol->GetMacAddress (MacAddress);
+  if (EFI_ERROR (Status)) {
+DEBUG ((DEBUG_ERROR, "%a: failed to retrieve MAC address\n", 
__FUNCTION__));
+return;
+  }
+
+  Retval = fdt_setprop (mFdtImage, Node, "mac-address", MacAddress,
+sizeof MacAddress);
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "%a: failed to create 'mac-address' property (%d)\n",
+  __FUNCTION__, Retval));
+return;
+  }
+
+  DEBUG ((DEBUG_INFO, "%a: setting MAC address to 
%02x:%02x:%02x:%02x:%02x:%02x\n",
+__FUNCTION__, MacAddress[0], MacAddress[1], MacAddress[2], MacAddress[3],
+MacAddress[4], MacAddress[5]));
+}
+
+STATIC
+VOID
+CleanMemoryNodes (
+  VOID
+  )
+{
+  INTN Node;
+  INT32 Retval;
+
+  Node = fdt_path_offset (mFdtImage, "/memory");
+  if (Node < 0) {
+return;
+  }
+
+  /*
+   * Remove bogus memory nodes which can make the booted
+   * OS go crazy and ignore the UEFI map.
+   */
+  DEBUG ((DEBUG_INFO, "Removing bogus /memory\n"));
+  Retval = fdt_del_node (mFdtImage, Node);
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Failed to remove /memory\n"));
+  }
+}
+
+STATIC
+VOID
+SanitizePSCI (
+  VOID
+  )
+{
+  INTN Node;
+  INTN Root;
+  INT32 Retval;
+
+  Root = fdt_path_offset (mFdtImage, "/");
+  ASSERT (Root >= 0);
+  if (Root < 0) {
+return;
+  }
+
+  Node = fdt_path_offset (mFdtImage, "/psci");
+  if (Node < 0) {
+Node = fdt_add_subnode (mFdtImage, Root, "psci");
+  }
+
+  ASSERT (Node >= 0);
+  if (Node < 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't find/create /psci\n"));
+return;
+  }
+
+  Retval = fdt_setprop_string (mFdtImage, Node, "compatible", "arm,psci-1.0");
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't set /psci compatible property\n"));
+return;
+  }
+
+  Retval = fdt_setprop_string (mFdtImage, Node, "method", "smc");
+  if (Retval != 0) {
+DEBUG ((DEBUG_ERROR, "Couldn't set /psci method property\n"));
+return;
+  }
+
+  Root = fdt_path_offset (mFdtImage, "/cpus");
+  if (Root < 0) {
+DEBUG ((DEBUG_ERROR, "No CPUs to update with PSCI enable-method?\n"));
+return;
+  }
+
+  Node = fdt_first_subnode (mFdtImage, Root);
+  while (Node >= 0) {
+if (fdt_setprop_string (mFdtImage, Node, "enable-method", "psci") != 0) {
+  DEBUG ((DEBUG_ERROR, "Failed to update enable-method for a CPU\n"));
+  return;
+}
+
+fdt_delprop (mFdtImage, Node, "cpu-release-addr");
+Node = fdt_next_subnode (mFdtImage, Node);
+  }
+}
+
+STATIC
+VOID
+CleanSimpleFramebuffer (
+  VOID
+  )
+{
+  INTN Node;
+  INT32 Retval;
+
+  /*
+   * Should look for nodes by kind and remove aliases
+   * by matching against device.
+   */
+  Node = fdt_path_offset (mFdtImage, "display0");
+  if (Node < 0) {
+return;
+  }
+
+  /*
+   * Remove bogus GPU-injected simple-framebuffer, which
+   * doesn't reflect the framebuffer built by UEFI.
+   */
+  DEBUG ((DEBUG_INF

[edk2] [PATCH v3 edk2-platforms 12/23] Platform/Raspberry/Pi3: Add NV storage driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c   | 196 

 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FvbInfo.c  | 115 
+++
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockService.c  | 971 

 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockService.h  | 217 
+
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.c   | 331 
+++
 Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.inf |  93 
++
 6 files changed, 1923 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c 
b/Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c
new file mode 100644
index ..0e8cd516f65e
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/VarBlockServiceDxe/FileIo.c
@@ -0,0 +1,196 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2007-2009, Intel Corporation. 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 "VarBlockService.h"
+
+
+EFI_STATUS
+FileWrite (
+  IN EFI_FILE_PROTOCOL *File,
+  IN UINTN Offset,
+  IN UINTN Buffer,
+  IN UINTN Size
+  )
+{
+  EFI_STATUS Status;
+
+  Status = File->SetPosition (File, Offset);
+  ASSERT_EFI_ERROR (Status);
+  if (!EFI_ERROR (Status)) {
+Status = File->Write (File, &Size, (VOID*)Buffer);
+ASSERT_EFI_ERROR (Status);
+  }
+  return Status;
+}
+
+
+VOID
+FileClose (
+  IN  EFI_FILE_PROTOCOL *File
+  )
+{
+  File->Flush (File);
+  File->Close (File);
+}
+
+
+EFI_STATUS
+FileOpen (
+  IN  EFI_DEVICE_PATH_PROTOCOL *Device,
+  IN  CHAR16 *MappedFile,
+  OUT EFI_FILE_PROTOCOL **File,
+  IN  UINT64 OpenMode
+  )
+{
+  EFI_HANDLEHandle;
+  EFI_FILE_HANDLE   Root;
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL   *Volume;
+  EFI_STATUSStatus;
+
+  *File = NULL;
+
+  Status = gBS->LocateDevicePath (
+  &gEfiSimpleFileSystemProtocolGuid,
+  &Device,
+  &Handle
+);
+
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  Status = gBS->HandleProtocol (
+  Handle,
+  &gEfiSimpleFileSystemProtocolGuid,
+  (VOID**)&Volume
+);
+  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  //
+  // Open the root directory of the volume
+  //
+  Root = NULL;
+  Status = Volume->OpenVolume (
+ Volume,
+ &Root
+   );
+  ASSERT_EFI_ERROR (Status);
+  ASSERT (Root != NULL);
+
+  //
+  // Open file
+  //
+  Status = Root->Open (
+   Root,
+   File,
+   MappedFile,
+   OpenMode,
+   0
+ );
+  if (EFI_ERROR (Status)) {
+*File = NULL;
+  }
+
+  //
+  // Close the Root directory
+  //
+  Root->Close (Root);
+  return Status;
+}
+
+
+EFI_STATUS
+CheckStore (
+  IN  EFI_HANDLE SimpleFileSystemHandle,
+  OUT EFI_DEVICE_PATH_PROTOCOL **Device
+  )
+{
+  EFI_STATUS Status;
+  EFI_BLOCK_IO_PROTOCOL *BlkIo;
+  EFI_FILE_PROTOCOL *File;
+
+  *Device = NULL;
+  Status = gBS->HandleProtocol (
+  SimpleFileSystemHandle,
+  &gEfiBlockIoProtocolGuid,
+  (VOID*)&BlkIo
+);
+
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+  if (!BlkIo->Media->MediaPresent) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media not present!\n"));
+Status = EFI_NO_MEDIA;
+goto ErrHandle;
+  }
+  if (BlkIo->Media->ReadOnly) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media is read-only!\n"));
+Status = EFI_ACCESS_DENIED;
+goto ErrHandle;
+  }
+
+  Status = FileOpen (DevicePathFromHandle (SimpleFileSystemHandle),
+ mFvInstance->MappedFile, &File,
+ EFI_FILE_MODE_READ);
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+
+  /* We found it! Maybe do more checks...? */
+
+  FileClose (File);
+  *Device = DuplicateDevicePath (DevicePathFromHandle 
(SimpleFileSystemHandle));
+
+  ASSERT (*Device != NULL);
+
+ErrHandle:
+  return Status;
+}
+
+
+EFI_STATUS
+CheckStoreExists (
+  IN  EFI_DEVICE_PATH_PROTOCOL *Device
+  )
+{
+  EFI_HANDLE Handle;
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
+  EFI_STATUS Status;
+
+  Status = gBS->

[edk2] [PATCH v3 edk2-platforms 11/23] Platform/Raspberry/Pi3: Add console driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c 
|  183 ++
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsole.c   
| 1837 
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsole.h   
|  591 +++
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxe.inf  
|   74 +
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxe.uni  
|   18 +
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/GraphicsConsoleDxeExtra.uni 
|   18 +
 Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/NewFont.c   
|  287 +++
 Platform/Raspberry/Pi3/Include/Protocol/ExtendedTextOut.h 
|   36 +
 8 files changed, 3044 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c
new file mode 100644
index ..3ae639ad3d97
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/GraphicsConsoleDxe/ComponentName.c
@@ -0,0 +1,183 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2006-2016, Intel Corporation. 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 "GraphicsConsole.h"
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL  
gGraphicsConsoleComponentName = {
+  GraphicsConsoleComponentNameGetDriverName,
+  GraphicsConsoleComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL 
gGraphicsConsoleComponentName2 = {
+  
(EFI_COMPONENT_NAME2_GET_DRIVER_NAME)GraphicsConsoleComponentNameGetDriverName,
+  
(EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)GraphicsConsoleComponentNameGetControllerName,
+  "en"
+};
+
+
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE 
mGraphicsConsoleDriverNameTable[] = {
+  {
+"eng;en",
+(CHAR16*)L"Graphics Console Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+EFI_STATUS
+EFIAPI
+GraphicsConsoleComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+   Language,
+   This->SupportedLanguages,
+   

[edk2] [PATCH v3 edk2-platforms 10/23] Platform/Raspberry/Pi3: Add display driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c | 222 +++
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/DisplayDxe.c| 606 

 Platform/Raspberry/Pi3/Drivers/DisplayDxe/DisplayDxe.h|  42 ++
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/DisplayDxe.inf  |  71 +++
 Platform/Raspberry/Pi3/Drivers/DisplayDxe/Screenshot.c| 375 
 5 files changed, 1316 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c 
b/Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c
new file mode 100644
index ..9a84aea511f4
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/DisplayDxe/ComponentName.c
@@ -0,0 +1,222 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2006-2016, Intel Corporation. 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 "DisplayDxe.h"
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  );
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL *This,
+  IN  EFI_HANDLE  ControllerHandle,
+  IN  EFI_HANDLE  ChildHandle,
+  IN  CHAR8   *Language,
+  OUT CHAR16  **ControllerName
+  );
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
+  ComponentNameGetDriverName,
+  ComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)ComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)ComponentNameGetControllerName,
+  "en"
+};
+
+
+STATIC EFI_UNICODE_STRING_TABLE mDriverName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi Display Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mDeviceName[] = {
+  {
+"eng;en",
+(CHAR16*)L"Raspberry Pi Framebuffer"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT 

[edk2] [PATCH v3 edk2-platforms 09/23] Platform/Raspberry/Pi3: Add SMBIOS driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c   | 903 

 Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.inf |  56 ++
 2 files changed, 959 insertions(+)

diff --git 
a/Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c 
b/Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c
new file mode 100644
index ..7707763e4332
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/PlatformSmbiosDxe/PlatformSmbiosDxe.c
@@ -0,0 +1,903 @@
+/** @file
+ *
+ *  Static SMBIOS Table for ARM platform
+ *  Derived from EmulatorPkg package
+ *
+ *  Note SMBIOS 2.7.1 Required structures:
+ *  BIOS Information (Type 0)
+ *  System Information (Type 1)
+ *  Board Information (Type 2)
+ *  System Enclosure (Type 3)
+ *  Processor Information (Type 4) - CPU Driver
+ *  Cache Information (Type 7) - For cache that is external to processor
+ *  System Slots (Type 9) - If system has slots
+ *  Physical Memory Array (Type 16)
+ *  Memory Device (Type 17) - For each socketed system-memory Device
+ *  Memory Array Mapped Address (Type 19) - One per contiguous block per 
Physical Memroy Array
+ *  System Boot Information (Type 32)
+ *
+ *  Copyright (c) 2017-2018, Andrey Warkentin 
+ *  Copyright (c) 2013, Linaro.org
+ *  Copyright (c) 2012, Apple Inc. All rights reserved.
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+/***
+SMBIOS data definition  TYPE0  BIOS Information
+/
+SMBIOS_TABLE_TYPE0 mBIOSInfoType0 = {
+  { EFI_SMBIOS_TYPE_BIOS_INFORMATION, sizeof (SMBIOS_TABLE_TYPE0), 0 },
+  1,// Vendor String
+  2,// BiosVersion String
+  0x0,  // BiosSegment
+  3,// BiosReleaseDate String
+  0x1F, // BiosSize
+  { // BiosCharacteristics
+0,//  Reserved  :2;  ///< Bits 0-1.
+0,//  Unknown   :1;
+0,//  BiosCharacteristicsNotSupported   :1;
+0,//  IsaIsSupported:1;
+0,//  McaIsSupported:1;
+0,//  EisaIsSupported   :1;
+0,//  PciIsSupported:1;
+0,//  PcmciaIsSupported :1;
+0,//  PlugAndPlayIsSupported:1;
+0,//  ApmIsSupported:1;
+0,//  BiosIsUpgradable  :1;
+0,//  BiosShadowingAllowed  :1;
+0,//  VlVesaIsSupported :1;
+0,//  EscdSupportIsAvailable:1;
+0,//  BootFromCdIsSupported :1;
+1,//  SelectableBootIsSupported :1;
+0,//  RomBiosIsSocketed :1;
+0,//  BootFromPcmciaIsSupported :1;
+0,//  EDDSpecificationIsSupported   :1;
+0,//  JapaneseNecFloppyIsSupported  :1;
+0,//  JapaneseToshibaFloppyIsSupported  :1;
+0,//  Floppy525_360IsSupported  :1;
+0,//  Floppy525_12IsSupported   :1;
+0,//  Floppy35_720IsSupported   :1;
+0,//  Floppy35_288IsSupported   :1;
+0,//  PrintScreenIsSupported:1;
+0,//  Keyboard8042IsSupported   :1;
+0,//  SerialIsSupported :1;
+0,//  PrinterIsSupported:1;
+0,//  CgaMonoIsSupported:1;
+0,//  NecPc98   :1;
+0 //  ReservedForVendor :32; ///< Bits 32-63. Bits 
32-47 reserved for BIOS vendor
+///< and bits 48-63 reserved for System Vendor.
+  },
+  {   // BIOSCharacteristicsExtensionBytes[]
+0x01, //  AcpiIsSupported   :1;
+  //  UsbLegacyIsSupported  :1;
+  //  AgpIsSupported:1;
+  //  I2OBootIsSupported:1;
+  //  Ls120BootIsSupported  :1;
+  //  AtapiZipDriveBootIsSupported  :1;
+  //  

[edk2] [PATCH v3 edk2-platforms 08/23] Platform/Raspberry/Pi3: Add platform config driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c| 350 

 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.inf  |  77 +
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxeFormSetGuid.h |  23 ++
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxeHii.uni   | 100 ++
 Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxeHii.vfr   | 306 
+
 5 files changed, 856 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c 
b/Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c
new file mode 100644
index ..9c0e2214b7f5
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/ConfigDxe/ConfigDxe.c
@@ -0,0 +1,350 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "ConfigDxeFormSetGuid.h"
+
+extern UINT8 ConfigDxeHiiBin[];
+extern UINT8 ConfigDxeStrings[];
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+typedef struct {
+  VENDOR_DEVICE_PATH VendorDevicePath;
+  EFI_DEVICE_PATH_PROTOCOL End;
+} HII_VENDOR_DEVICE_PATH;
+
+STATIC HII_VENDOR_DEVICE_PATH mVendorDevicePath = {
+  {
+{
+  HARDWARE_DEVICE_PATH,
+  HW_VENDOR_DP,
+  {
+(UINT8)(sizeof (VENDOR_DEVICE_PATH)),
+(UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
+  }
+},
+CONFIGDXE_FORM_SET_GUID
+  },
+  {
+END_DEVICE_PATH_TYPE,
+END_ENTIRE_DEVICE_PATH_SUBTYPE,
+{
+  (UINT8)(END_DEVICE_PATH_LENGTH),
+  (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
+}
+  }
+};
+
+
+STATIC EFI_STATUS
+InstallHiiPages (
+  VOID
+  )
+{
+  EFI_STATUS Status;
+  EFI_HII_HANDLE HiiHandle;
+  EFI_HANDLE DriverHandle;
+
+  DriverHandle = NULL;
+  Status = gBS->InstallMultipleProtocolInterfaces (&DriverHandle,
+  &gEfiDevicePathProtocolGuid,
+  &mVendorDevicePath,
+  NULL);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  HiiHandle = HiiAddPackages (&gConfigDxeFormSetGuid,
+DriverHandle,
+ConfigDxeStrings,
+ConfigDxeHiiBin,
+NULL);
+
+  if (HiiHandle == NULL) {
+gBS->UninstallMultipleProtocolInterfaces (DriverHandle,
+   &gEfiDevicePathProtocolGuid,
+   &mVendorDevicePath,
+   NULL);
+return EFI_OUT_OF_RESOURCES;
+  }
+  return EFI_SUCCESS;
+}
+
+
+STATIC EFI_STATUS
+SetupVariables (
+  VOID
+  )
+{
+  UINTN Size;
+  UINT32 Var32;
+  EFI_STATUS Status;
+
+  /*
+   * Create the vars with default value.
+   * If we don't, forms won't be able to update.
+   */
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"CpuClock",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdCpuClock, PcdGet32 (PcdCpuClock));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"SdIsArasan",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdSdIsArasan, PcdGet32 (PcdSdIsArasan));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcDisableMulti",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcDisableMulti, PcdGet32 (PcdMmcDisableMulti));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcForce1Bit",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForce1Bit, PcdGet32 (PcdMmcForce1Bit));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcForceDefaultSpeed",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForceDefaultSpeed, PcdGet32 (PcdMmcForceDefaultSpeed));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable (L"MmcSdDefaultSpeedMHz",
+  &gConfigDxeFormSetGuid,
+  NULL, &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcSdDefaultSpeedMHz, PcdGet32 (PcdMmcSdDefaultSp

[edk2] [PATCH v3 edk2-platforms 07/23] Platform/Raspberry/Pi3: Add firmware driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 

Reviewed-by: Ard Biesheuvel 
---
 Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c   | 1084 

 Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.inf |   49 +
 Platform/Raspberry/Pi3/Include/Protocol/RpiFirmware.h|  131 +++
 3 files changed, 1264 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c 
b/Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
new file mode 100644
index ..d330e45fdc4c
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
@@ -0,0 +1,1084 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrei Warkentin 
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include 
+
+//
+// The number of statically allocated buffer pages
+//
+#define NUM_PAGES   1
+
+//
+// The number of iterations to perform when waiting for the mailbox
+// status to change
+//
+#define MAX_TRIES   0x10
+
+STATIC VOID  *mDmaBuffer;
+STATIC VOID  *mDmaBufferMapping;
+STATIC UINTN mDmaBufferBusAddress;
+
+STATIC SPIN_LOCK mMailboxLock;
+
+STATIC
+BOOLEAN
+DrainMailbox (
+  VOID
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if (Val & (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+BOOLEAN
+MailboxWaitForStatusCleared (
+  IN  UINTN   StatusMask
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if ((Val & StatusMask) == 0) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+EFI_STATUS
+MailboxTransaction (
+  INUINTN   Length,
+  INUINTN   Channel,
+  OUT   UINT32  *Result
+  )
+{
+  if (Channel >= BCM2836_MBOX_NUM_CHANNELS) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  if (!DrainMailbox ()) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for mailbox to drain\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Wait for the 'output register full' bit to become clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_FULL)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for outbox to become empty\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Start the mailbox transaction
+  //
+  MmioWrite32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_WRITE_OFFSET,
+(UINT32)((UINTN)mDmaBufferBusAddress | Channel));
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Wait for the 'input register empty' bit to clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for inbox to become full\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Read back the result
+  //
+  ArmDataSynchronizationBarrier ();
+  *Result = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  ArmDataSynchronizationBarrier ();
+
+  return EFI_SUCCESS;
+}
+
+#pragma pack(1)
+typedef struct {
+  UINT32BufferSize;
+  UINT32Response;
+} RPI_FW_BUFFER_HEAD;
+
+typedef struct {
+  UINT32TagId;
+  UINT32TagSize;
+  UINT32TagValueSize;
+} RPI_FW_TAG_HEAD;
+
+typedef struct {
+  UINT32DeviceId;
+  UINT32PowerState;
+} RPI_FW_POWER_STATE_TAG;
+
+typedef struct {
+  RPI_FW_BUFFER_HEADBufferHead;
+  RPI_FW_TAG_HEAD   TagHead;
+  RPI_FW_POWER_STATE_TAGTagBody;
+  UINT32EndTag;
+} RPI_FW_SET_POWER_STATE_CMD;
+#pragma pack()
+
+STATIC
+EFI_STATUS
+EFIAPI
+RpiFirmwareSetPowerState (
+  IN  UINT32DeviceId,
+  IN  BOOLEAN   PowerState,
+  IN  BOOLEAN   Wai

[edk2] [PATCH v3 edk2-platforms 06/23] Platform/Raspberry/Pi3: Add RTC library

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 
Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
   | 221 
 
Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
 |  43 
 2 files changed, 264 insertions(+)

diff --git 
a/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
 
b/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
new file mode 100644
index ..dea621ff03ef
--- /dev/null
+++ 
b/Platform/Raspberry/Pi3/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
@@ -0,0 +1,221 @@
+/** @file
+ *
+ *  Implement dummy EFI RealTimeClock runtime services.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/**
+   Returns the current time and date information, and the time-keeping 
capabilities
+   of the virtual RTC.
+
+   For simplicity, this LibGetTime does not report Years/Months, instead it 
will only report current
+   Day, Hours, Minutes and Seconds starting from the begining of CPU up-time. 
Otherwise, a more
+   complex logic will be required to account for leap years and days/month 
differences.
+
+   @param  Time  A pointer to storage to receive a snapshot of 
the current time.
+   @param  Capabilities  An optional pointer to a buffer to receive 
the real time clock
+   device's capabilities.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER Time is NULL.
+   @retval EFI_DEVICE_ERROR  The time could not be retrieved due to 
hardware error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibGetTime (
+  OUT EFI_TIME   *Time,
+  OUT EFI_TIME_CAPABILITIES  *Capabilities
+  )
+{
+  UINTN DataSize;
+  UINT64 Counter;
+  EFI_STATUS Status;
+  UINTN ElapsedSeconds;
+  UINT32 Remainder;
+  UINT32 Freq = ArmGenericTimerGetTimerFreq ();
+
+  if (Time == NULL) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Depend on ARM generic timer to report date/time relative to the
+  // start of CPU timer counting where date and time will always
+  // be relative to the date/time 1/1/1900 00H:00M:00S
+  //
+
+  ASSERT (Freq != 0);
+  if (Freq == 0) {
+return EFI_DEVICE_ERROR;
+  }
+
+  if (Capabilities) {
+Capabilities->Accuracy = 0;
+Capabilities->Resolution = Freq;
+Capabilities->SetsToZero = FALSE;
+  }
+
+  DataSize = sizeof (UINTN);
+  ElapsedSeconds = 0;
+  Status = EfiGetVariable (L"RtcEpochSeconds",
+ &gEfiCallerIdGuid,
+ NULL,
+ &DataSize,
+ &ElapsedSeconds);
+  if (EFI_ERROR (Status)) {
+ElapsedSeconds = PcdGet64 (PcdBootEpochSeconds);
+  }
+  Counter = GetPerformanceCounter ();
+  ElapsedSeconds += DivU64x32Remainder (Counter, Freq, &Remainder);
+  EpochToEfiTime (ElapsedSeconds, Time);
+
+  //
+  // Frequency < 0x1, so Remainder < 0x1, then (Remainder * 
1,000,000,000)
+  // will not overflow 64-bit.
+  //
+  Time->Nanosecond = DivU64x32 (MultU64x64 ((UINT64)Remainder, 10U), 
Freq);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+   Sets the current local time and date information.
+
+   @param  Time  A pointer to the current time.
+
+   @retval EFI_SUCCESS   The operation completed successfully.
+   @retval EFI_INVALID_PARAMETER A time field is out of range.
+   @retval EFI_DEVICE_ERROR  The time could not be set due due to hardware 
error.
+
+**/
+EFI_STATUS
+EFIAPI
+LibSetTime (
+  IN EFI_TIME  *Time
+  )
+{
+  UINTN Epoch;
+
+  if (!IsTimeValid (Time)) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  Epoch = EfiTimeToEpoch (Time);
+  return EfiSetVariable (L"RtcEpochSeconds", &gEfiCallerIdGuid,
+   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+   EFI_VARIABLE_RUNTIME_ACCESS |
+   EFI_VARIABLE_NON_VOLATILE,
+   sizeof (Epoch),
+   &Epoch);
+}
+
+
+/**
+   Returns the current wakeup alarm clock setting.
+
+   @param  Enabled   Indicates if the alarm is currently enabled 
or disabled.
+   @param  Pending   Indicates if the alarm signal is pending and 
requires acknowledgement.
+   @param  Time  The current alarm setting.
+
+   @retval EFI_SUCCESS   The alarm se

[edk2] [PATCH v3 edk2-platforms 05/23] Platform/Raspberry/Pi3: Add platform library

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h  | 108 
+
 Platform/Raspberry/Pi3/Library/PlatformLib/AArch64/RaspberryPiHelper.S | 107 
+
 Platform/Raspberry/Pi3/Library/PlatformLib/PlatformLib.inf |  64 

 Platform/Raspberry/Pi3/Library/PlatformLib/RaspberryPi.c   |  99 

 Platform/Raspberry/Pi3/Library/PlatformLib/RaspberryPiMem.c| 160 

 5 files changed, 538 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h 
b/Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h
new file mode 100644
index ..8547ad05ba61
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Include/IndustryStandard/RpiMbox.h
@@ -0,0 +1,108 @@
+/** @file
+ *
+ * Copyright (c) 2019, Pete Batard 
+ * Copyright (c) 2016, Linaro Limited. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * Neither the name of ARM nor the names of its contributors may be used
+ * to endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ **/
+
+#ifndef __RASPBERRY_PI_MAILBOX_H__
+#define __RASPBERRY_PI_MAILBOX_H__
+
+/* Mailbox channels */
+#define RPI_MBOX_PM_CHANNEL   0
+#define RPI_MBOX_FB_CHANNEL   1
+#define RPI_MBOX_VUART_CHANNEL2
+#define RPI_MBOX_VCHIQ_CHANNEL3
+#define RPI_MBOX_LED_CHANNEL  4
+#define RPI_MBOX_BUTTON_CHANNEL   5
+#define RPI_MBOX_TOUCHSCREEN_CHANNEL  6
+/* Request from ARM for response by VideoCore */
+#define RPI_MBOX_VC_CHANNEL   8
+/* Request from VideoCore for response by ARM */
+#define RPI_MBOX_ARM_CHANNEL  9
+
+#define RPI_MBOX_RESP_SUCCESS 0x8000
+#define RPI_MBOX_RESP_FAILURE 0x8001
+
+#define RPI_MBOX_VALUE_SIZE_RESPONSE_MASK BIT31
+
+#define RPI_MBOX_GET_REVISION 0x0001
+#define RPI_MBOX_GET_BOARD_MODEL  0x00010001
+#define RPI_MBOX_GET_BOARD_REVISION   0x00010002
+#define RPI_MBOX_GET_MAC_ADDRESS  0x00010003
+#define RPI_MBOX_GET_BOARD_SERIAL 0x00010004
+#define RPI_MBOX_GET_ARM_MEMSIZE  0x00010005
+
+#define RPI_MBOX_SET_POWER_STATE  0x00028001
+
+#define RPI_MBOX_POWER_STATE_SDHCI0x
+#define RPI_MBOX_POWER_STATE_UART00x0001
+#define RPI_MBOX_POWER_STATE_UART10x0002
+#define RPI_MBOX_POWER_STATE_USB_HCD  0x0003
+#define RPI_MBOX_POWER_STATE_I2C0 0x0004
+#define RPI_MBOX_POWER_STATE_I2C1 0x0005
+#define RPI_MBOX_POWER_STATE_I2C2 0x0006
+#define RPI_MBOX_POWER_STATE_SPI  0x0007
+#define RPI_MBOX_POWER_STATE_CCP2TX   0x0008
+
+#define RPI_MBOX_GET_CLOCK_RATE   0x00030002
+#define RPI_MBOX_GET_MAX_CLOCK_RATE   0x00030004
+#define RPI_MBOX_GET_MIN_CLOCK_RATE   0x00030007
+
+#define RPI_MBOX_SET_CLOCK_RATE   

[edk2] [PATCH v3 edk2-platforms 04/23] Platform/Raspberry/Pi3: Add reset and memory init libraries

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c   | 162 

 Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf |  51 
++
 Platform/Raspberry/Pi3/Library/ResetLib/ResetLib.c   | 104 
+
 Platform/Raspberry/Pi3/Library/ResetLib/ResetLib.inf |  46 
++
 4 files changed, 363 insertions(+)

diff --git a/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c 
b/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
new file mode 100644
index ..903364e08b15
--- /dev/null
+++ b/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
@@ -0,0 +1,162 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrey Warkentin 
+ *  Copyright (c) 2011-2015, ARM Limited. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+extern UINT64 mSystemMemoryEnd;
+
+VOID
+BuildMemoryTypeInformationHob (
+  VOID
+  );
+
+STATIC
+VOID
+InitMmu (
+  IN ARM_MEMORY_REGION_DESCRIPTOR  *MemoryTable
+  )
+{
+  RETURN_STATUS Status;
+
+  //Note: Because we called PeiServicesInstallPeiMemory() before to call 
InitMmu() the MMU Page Table
+  //  resides in DRAM (even at the top of DRAM as it is the first 
permanent memory allocation)
+  Status = ArmConfigureMmu (MemoryTable, NULL, NULL);
+  if (EFI_ERROR (Status)) {
+DEBUG ((DEBUG_ERROR, "Error: Failed to enable MMU\n"));
+  }
+}
+
+STATIC
+VOID
+AddRuntimeServicesRegion (
+  IN ARM_MEMORY_REGION_DESCRIPTOR *Desc
+)
+{
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+Desc->PhysicalBase,
+Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiRuntimeServicesData
+  );
+}
+
+STATIC
+VOID
+AddReservedMemoryRegion (
+  IN ARM_MEMORY_REGION_DESCRIPTOR *Desc
+  )
+{
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+Desc->PhysicalBase,
+Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiReservedMemoryType
+  );
+}
+
+/*++
+
+Routine Description:
+
+
+
+Arguments:
+
+  FileHandle  - Handle of the file being invoked.
+  PeiServices - Describes the list of possible PEI Services.
+
+Returns:
+
+  Status -  EFI_SUCCESS if the boot mode could be set
+
+--*/
+EFI_STATUS
+EFIAPI
+MemoryPeim (
+  IN EFI_PHYSICAL_ADDRESS  UefiMemoryBase,
+  IN UINT64UefiMemorySize
+  )
+{
+  ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;
+
+  // Get Virtual Memory Map from the Platform Library
+  ArmPlatformGetVirtualMemoryMap (&MemoryTable);
+
+  // Ensure PcdSystemMemorySize has been set
+  ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);
+
+  // FD without variable store
+  AddReservedMemoryRegion (&MemoryTable[0]);
+
+  // Variable store.
+  AddRuntimeServicesRegion (&MemoryTable[1]);
+
+  // Trusted Firmware region
+  AddReservedMemoryRegion (&MemoryTable[2]);
+
+  // Usable memory.
+  BuildResourceDescriptorHob (
+EFI_RESOURCE_SYSTEM_MEMORY,
+EFI_RESOURCE_ATTRIBUTE_PRESENT |
+EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+EFI_RESOURCE_ATTRIBUTE_TESTED,
+MemoryTable[3].PhysicalBase,
+MemoryTable[3].Length
+  );
+
+  AddReservedMemoryRegion (&MemoryTable[4]);
+
+  // Build Memory Allocation Hob
+  InitMmu (MemoryTable);
+
+  if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {
+// Optional feature that helps prevent EFI memory map fragmentation.
+BuildMemoryTypeInformationHob ();
+  }
+
+  return EFI_SUCCESS;
+}
diff --git 
a/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf 
b/Platform/Raspberry/Pi3/Library/MemoryInitPeiLib/MemoryInit

[edk2] [PATCH v3 edk2-platforms 03/23] Platform/Raspberry/Pi3: Add ACPI tables

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h   |  82 
 Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf |  51 ++
 Platform/Raspberry/Pi3/AcpiTables/Csrt.aslc  | 332 +
 Platform/Raspberry/Pi3/AcpiTables/Dbg2.aslc  |  34 ++
 Platform/Raspberry/Pi3/AcpiTables/Dsdt.asl   | 511 
 Platform/Raspberry/Pi3/AcpiTables/Fadt.aslc  |  52 ++
 Platform/Raspberry/Pi3/AcpiTables/Gtdt.aslc  |  33 ++
 Platform/Raspberry/Pi3/AcpiTables/Madt.aslc  |  62 +++
 Platform/Raspberry/Pi3/AcpiTables/Pep.asl|  95 
 Platform/Raspberry/Pi3/AcpiTables/Pep.c  |  84 
 Platform/Raspberry/Pi3/AcpiTables/Pep.h  | 126 +
 Platform/Raspberry/Pi3/AcpiTables/Rhpx.asl   | 201 
 Platform/Raspberry/Pi3/AcpiTables/Sdhc.asl   | 105 
 Platform/Raspberry/Pi3/AcpiTables/Spcr.asl   |  53 ++
 Platform/Raspberry/Pi3/AcpiTables/Uart.asl   | 158 ++
 15 files changed, 1979 insertions(+)

diff --git a/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h 
b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h
new file mode 100644
index ..be28b6decefd
--- /dev/null
+++ b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.h
@@ -0,0 +1,82 @@
+/** @file
+ *
+ *  RPi3 defines for constructing ACPI tables
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ **/
+
+#ifndef __ACPITABLES_H__
+#define __ACPITABLES_H__
+
+#include 
+
+#define EFI_ACPI_OEM_ID   {'M','C','R','S','F','T'} // 
OEMID 6 bytes long
+#define EFI_ACPI_OEM_TABLE_ID SIGNATURE_64 
('R','P','I','3','E','D','K','2') // OEM table id 8 bytes long
+#define EFI_ACPI_OEM_REVISION 0x02000820
+#define EFI_ACPI_CREATOR_ID   SIGNATURE_32 ('R','P','I','3')
+#define EFI_ACPI_CREATOR_REVISION 0x0097
+
+#define EFI_ACPI_VENDOR_IDSIGNATURE_32 ('M','S','F','T')
+#define EFI_ACPI_CSRT_REVISION0x0005
+#define EFI_ACPI_CSRT_DEVICE_ID_DMA   0x0009 // Fixed id
+#define EFI_ACPI_CSRT_RESOURCE_ID_IN_DMA_GRP  0x0 // Count up from 0
+
+#define RPI3_DMA_CHANNEL_COUNT10 // All 10 DMA channels are 
listed, including the reserved ones
+#define RPI3_DMA_USED_CHANNEL_COUNT   5  // Use 5 DMA channels
+
+#define EFI_ACPI_5_0_CSRT_REVISION0x
+
+typedef enum
+{
+  EFI_ACPI_CSRT_RESOURCE_TYPE_RESERVED,   // 0
+  EFI_ACPI_CSRT_RESOURCE_TYPE_INTERRUPT,  // 1
+  EFI_ACPI_CSRT_RESOURCE_TYPE_TIMER,  // 2
+  EFI_ACPI_CSRT_RESOURCE_TYPE_DMA,// 3
+  EFI_ACPI_CSRT_RESOURCE_TYPE_CACHE,  // 4
+}
+CSRT_RESOURCE_TYPE;
+
+typedef enum
+{
+  EFI_ACPI_CSRT_RESOURCE_SUBTYPE_DMA_CHANNEL, // 0
+  EFI_ACPI_CSRT_RESOURCE_SUBTYPE_DMA_CONTROLLER   // 1
+}
+CSRT_DMA_SUBTYPE;
+
+//
+// CSRT Resource Group header 24 bytes long
+//
+typedef struct
+{
+  UINT32 Length;  // Length
+  UINT32 VendorID;// 4 bytes
+  UINT32 SubVendorId; // 4 bytes
+  UINT16 DeviceId;// 2 bytes
+  UINT16 SubdeviceId; // 2 bytes
+  UINT16 Revision;// 2 bytes
+  UINT16 Reserved;// 2 bytes
+  UINT32 SharedInfoLength;// 4 bytes
+} EFI_ACPI_5_0_CSRT_RESOURCE_GROUP_HEADER;
+
+//
+// CSRT Resource Descriptor 12 bytes total
+//
+typedef struct
+{
+  UINT32 Length;  // 4 bytes
+  UINT16 ResourceType;// 2 bytes
+  UINT16 ResourceSubType; // 2 bytes
+  UINT32 UID; // 4 bytes
+} EFI_ACPI_5_0_CSRT_RESOURCE_DESCRIPTOR_HEADER;
+
+#endif // __ACPITABLES_H__
diff --git a/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf 
b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf
new file mode 100644
index ..53095f46fdd5
--- /dev/null
+++ b/Platform/Raspberry/Pi3/AcpiTables/AcpiTables.inf

[edk2] [PATCH v3 edk2-platforms 02/23] Silicon/Broadcom/Bcm283x: Add GpioLib

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c  | 88 

 Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf| 39 +
 Silicon/Broadcom/Include/IndustryStandard/Bcm2836Gpio.h | 60 +
 3 files changed, 187 insertions(+)

diff --git a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c 
b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c
new file mode 100644
index ..eb6accafb01e
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c
@@ -0,0 +1,88 @@
+/** @file
+ *
+ *  GPIO manipulation.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+STATIC
+VOID
+GpioFSELModify (
+  IN  UINTN RegIndex,
+  IN  UINT32 ModifyMask,
+  IN  UINT32 FunctionMask
+  )
+{
+  UINT32 Val;
+  EFI_PHYSICAL_ADDRESS Reg;
+
+  Reg = RegIndex * sizeof (UINT32) + GPIO_GPFSEL0;
+
+  ASSERT (Reg <= GPIO_GPFSEL5);
+  ASSERT ((~ModifyMask & FunctionMask) == 0);
+
+  Val = MmioRead32 (Reg);
+  Val &= ~ModifyMask;
+  Val |= FunctionMask;
+  MmioWrite32 (Reg, Val);
+}
+
+VOID
+GpioPinFuncSet (
+  IN  UINTN Pin,
+  IN  UINTN Function
+  )
+{
+  UINTN RegIndex;
+  UINTN SelIndex;
+  UINT32 ModifyMask;
+  UINT32 FunctionMask;
+
+  ASSERT (Pin < GPIO_PINS);
+  ASSERT (Function <= GPIO_FSEL_MASK);
+
+  RegIndex = Pin / 10;
+  SelIndex = Pin % 10;
+
+  ModifyMask = GPIO_FSEL_MASK << (SelIndex * GPIO_FSEL_BITS_PER_PIN);
+  FunctionMask = Function << (SelIndex * GPIO_FSEL_BITS_PER_PIN);
+  GpioFSELModify (RegIndex, ModifyMask, FunctionMask);
+}
+
+UINTN
+GpioPinFuncGet (
+  IN  UINTN Pin
+  )
+{
+  UINT32 Val;
+  UINTN RegIndex;
+  UINTN SelIndex;
+  EFI_PHYSICAL_ADDRESS Reg;
+
+  ASSERT (Pin < GPIO_PINS);
+
+  RegIndex = Pin / 10;
+  SelIndex = Pin % 10;
+  Reg = RegIndex * sizeof (UINT32) + GPIO_GPFSEL0;
+
+  Val = MmioRead32 (Reg);
+  Val >>= SelIndex * GPIO_FSEL_BITS_PER_PIN;
+  Val &= GPIO_FSEL_MASK;
+  return Val;
+}
diff --git a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf 
b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf
new file mode 100644
index ..68ebe44e3d1c
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf
@@ -0,0 +1,39 @@
+#/** @file
+#
+#  Manipulate GPIOs.
+#
+#  Copyright (c) 2018, Andrei Warkentin 
+#
+#  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.
+#
+#**/
+
+[Defines]
+  INF_VERSION= 0x0001001A
+  BASE_NAME  = GpioLib
+  FILE_GUID  = B9F59B6B-B105-41C7-8F5A-2C60DD7FD7AB
+  MODULE_TYPE= BASE
+  VERSION_STRING = 1.0
+  LIBRARY_CLASS  = GpioLib
+
+[Sources]
+  GpioLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdePkg/MdePkg.dec
+  EmbeddedPkg/EmbeddedPkg.dec
+  Silicon/Broadcom/Bcm283x/Bcm283x.dec
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+  IoLib
+
+[Guids]
diff --git a/Silicon/Broadcom/Include/IndustryStandard/Bcm2836Gpio.h 
b/Silicon/Broadcom/Include/IndustryStandard/Bcm2836Gpio.h
new file mode 100644
index ..45774b639ca8
--- /dev/null
+++ b/Silicon/Broadcom/Include/IndustryStandard/Bcm2836Gpio.h
@@ -0,0 +1,60 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ **/
+
+#ifndef __BCM2836_GPIO_H__
+#define __BCM2836_GPIO_H__
+
+#define GPIO_BASE_ADDRESS  (BCM2836_SOC_REGISTERS + 0x0020)
+
+#define GPIO_GPFSEL0   (GPIO_BASE_ADDRESS + 0x00)
+#define GPIO_GPFSEL1   (GPIO_BASE_ADDRESS + 0x04)
+#define GPIO_GPFSEL2   (

[edk2] [PATCH v3 edk2-platforms 01/23] Silicon/Broadcom/Bcm282x: Add interrupt driver

2019-01-28 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Silicon/Broadcom/Bcm283x/Bcm283x.dec   |  23 ++
 Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c   | 367 

 Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.inf |  48 +++
 Silicon/Broadcom/Include/IndustryStandard/Bcm2836.h|  72 
 4 files changed, 510 insertions(+)

diff --git a/Silicon/Broadcom/Bcm283x/Bcm283x.dec 
b/Silicon/Broadcom/Bcm283x/Bcm283x.dec
new file mode 100644
index ..8ead5ca87f5d
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Bcm283x.dec
@@ -0,0 +1,23 @@
+## @file
+#
+#  Copyright (c) 2019, Pete Batard 
+#
+#  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.
+#
+##
+
+[Defines]
+  DEC_SPECIFICATION  = 0x0001001A
+  PACKAGE_NAME   = Bcm283xPkg
+  PACKAGE_GUID   = 900C0F44-1152-4FF9-B9C5-933E2918C831
+  PACKAGE_VERSION= 1.0
+
+[Includes]
+  Silicon/Broadcom/Include
diff --git a/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c 
b/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c
new file mode 100644
index ..9058aa94ffb9
--- /dev/null
+++ b/Silicon/Broadcom/Bcm283x/Drivers/InterruptDxe/InterruptDxe.c
@@ -0,0 +1,367 @@
+/** @file
+ *
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+//
+// This currently only implements support for the architected timer interrupts
+// on the per-CPU interrupt controllers.
+//
+#define NUM_IRQS(4)
+
+#ifdef MDE_CPU_AARCH64
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_AARCH64_IRQ
+#else
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_ARM_IRQ
+#endif
+
+STATIC CONST
+EFI_PHYSICAL_ADDRESS RegBase = FixedPcdGet32 (PcdInterruptBaseAddress);
+
+//
+// Notifications
+//
+STATIC EFI_EVENTmExitBootServicesEvent;
+STATIC HARDWARE_INTERRUPT_HANDLER   mRegisteredInterruptHandlers[NUM_IRQS];
+
+/**
+  Shutdown our hardware
+
+  DXE Core will disable interrupts and turn off the timer and disable 
interrupts
+  after all the event handlers have run.
+
+  @param[in]  Event   The Event that is being processed
+  @param[in]  Context Event Context
+**/
+STATIC
+VOID
+EFIAPI
+ExitBootServicesEvent (
+  IN EFI_EVENT  Event,
+  IN VOID   *Context
+  )
+{
+  // Disable all interrupts
+  MmioWrite32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 0);
+}
+
+/**
+  Enable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt enabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+EnableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT (FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioOr32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 1 << Source);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Disable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt disabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+DisableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT (FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioAnd32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, ~(1 << Source));
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Register Handler for the specified interrupt source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+  @param Handler  Callback for interrupt. NULL to unregister
+
+  @retval EFI_SUCCESS Source was updated to support Handler.
+  @retval EFI_DEVICE_E

[edk2] [PATCH v3 edk2-platforms 00/23] Platform/Raspberry: Add Raspberry Pi 3 support

2019-01-28 Thread Pete Batard
Changes applied to v3:

* As suggested, source is now split between Platform/Raspberry/Pi3 and
  Silicon/Broacdom/Bcm283x.
  Ideally, the SD/MMC drivers should also have gone to Silicon, but this is
  tricky to do, as these drivers depend on the custom (platform specific) Pi
  VideoCore to perform some actions such as driving the activity LEDs or
  fetching the current clock frequency, and therefore we'd need to abstract
  these and move part of the firmware protocol there (Mbox access, etc) so that
  we can get access these function calls. We looked into doing that, but the
  cost/benefit ratio is simply too high. So, considering that we now have a
  base for Platform/Silicon, I'd rather leave further factorization and
  abstraction to when we add another Bcm283x platform.
  For now then, only the GPIO library, Interrupt driver and the SD/MMC headers
  are to be found under Silicon.
* Source was overhauled to apply the EDK2 coding guidelines everywhere.
* .dsc/.fsf/.dec updated to use latest specs revision.
* utils.h: Remove it altogether (and also use ARRAY_SIZE() macro).
* MemoryInitPeiLib.c: Drop last 2 unused parameters in ArmConfigureMmu(), drop
  unneeded MMIO init and rename helper calls to something more explicit.
* FdtDxe.c: Use EfiBootServicesData in AllocatePages()
* Binary: Rename this directory to TrustedFirmware.
* Move DwUsbHostDxe driver to BSD license and therefore out of non-osi, since
  licensing matters have now been solved with all the parties, off-list.
* Move RpiFirmwareDxe earlier in the patch series
* Some internal ACPI fixes were applied (e.g. I2C table generation breakage due
  to a backslash in comments, etc.)
* A new PlatformUiAppLib was introduced, which is basically a helper for
  PlatformBootManagerLib. Also, the new default for boot manager is to boot
  into the UEFI shell. Of course this default boot order can be changed.
  NB: Because of these changes, we removed the Reviewed-By we previously got
  from Ard on the v2 version of this patch.
* Add a link to the new platform in the top-level readme.
* Our latest tests show that builds are stable with the latest GCC7 (Linaro
  2019.02 version), so we dropped the requirement for only GCC5 to be used.

Changes not applied to v3:

* MemoryInitPeiLib.c: We looked into moving ArmPlatformGetVirtualMemoryMap()
  from RaspberryPiMem.c into this file but it seemed more complex than we
  envisioned and, because we don't see the referencing of array entries as a
  showstopper, we hope that not carrying out this suggestion is okay.
* We tried to drop ResetLib altogether and use the reset notification event
  provided by the EDK2, but found that this resulted in a Linux kernel panic
  during reset, for which we have found no workaround and that seems to
  occur as soon as the relevant EDK2 drivers were included (i.e. crash happens
  even if the code isn't altered to use reset notification). Due to time
  constraints, and considering that ResetLib is very straightfoward, we decided
  that the best course of action is to keep ResetLib for now,,


Preamble:

Because of its price point, ease of use and availability, the Raspberry Pi is
undeniably one of the most successful ARM platform in existence today. Its
widespread adoption therefore makes it a perfect fit as an EDK2 platform.

However, up until now, the Raspberry Pi hasn't been supported as a bona fide
platform in our repository. This series of patches remedies that by introducing
the Raspberry Pi 3 Model B and Model B+ as a viable EDK2 platform.

Notes regarding non-OSI content:

* Even though the ARM Trusted Firmware binary blobs are subject to a
  BSD-3-Clause licence, which may be compatible with the EDK2 one, we chose
  to follow the lead of other platforms that provide ATF binaries in non OSI.
  Ultimately, once there is a new dot release of ATF, we plan to remove these
  binaries and point to a dot release build configuartion.
* The Device Tree binaries (and source descriptors) are subject to a GPLv2
  license, as per the ones published by the Raspberry Pi Foundation.
* The Logo source code is under an EDK2 license, but the logo itself, which
  we obtained authorisation to use from the Raspberry Pi Foundation itself
  after detailing our planned usage, is subject to the trademark licensing
  terms put forward by the Foundation.

Additional Notes:

* Detailed instructions on how to build and test the platform firmware are
  included in the Readme.md found at the root of the platform.
* As detailed in the Readme, the resulting platform firmware has been
  successfully used to install and run Linux OSes, such as Ubuntu 18.10, as
  well as Windows 10 1809 (*full* UI version, not IoT).

Regards,

/Pete

Pete Batard (20):
  Silicon/Broadcom/Bcm282x: Add interrupt driver
  Silicon/Broadcom/Bcm283x: Add GpioLib
  Platform/Raspberry/Pi3: Add ACPI tables
  Platform/Raspberry/Pi3: Add reset and memory init libraries
  Platform/Raspberry/Pi3: Add platform library

Re: [edk2] [PATCH v2 edk2-platforms 00/20] Platform/Broadcom: Add Raspberry Pi 3 support

2018-12-14 Thread Pete Batard

Hi Philippe, Hi Leif,

On 2018.12.14 16:36, Leif Lindholm wrote:

On Fri, Dec 14, 2018 at 05:14:05PM +0100, Philippe Mathieu-Daudé wrote:

I can certainly upload binary releases for the required ATF files in my
github clone of ATF, and then add links to that in the readme with the
instructions on how to rebuild ATF.

However, I'd rather wait to do that until there has been an official tagged
new release for ATF (which would be 2.1 at this stage, since our changes
have been applied after 2.0), as it'll look better for Pi users to see an
initial ATF serial output that says "BL<#>: v2.1(release)" instead of the
current "BL<#>: v2.0(release):v2.0-278-gc3859557"

How about this then: If ATF produces a formal release before this proposal
is integrated, I'll amend it to remove the ATF binary blobs and apply the
suggestion above, with the Atf/ build/link data moved out of non-osi. But if
they don't, I'll keep the proposal for ATF as is, and then submit a patch to
remove the non-osi data and apply the above at a later date, once there has
been a new ATF release.


Yeah, this certainly works for me.


I setup this Dockerfile [1] to have reproducible builds and avoid to
store those binaries in the non-osi repository, what do you think about
this approach?


Get off my lawn? :)


[1] available here:
https://gitlab.com/philmd/edk2-platforms/blob/raspi3-atf/Platform/RaspberryPi/RPi3/Arm-Tf/Dockerfile


I think it's a good thing to have, especially for something like the
Pi. I guess if that exists, we can trust people who prefer not to use
containers to be happy to follow the Readme, or grab binaries from
Pete's github?


That's one way to do it. As I indicated, as soon as ATF creates a 2.1 
tagged release, I will look into removing the binary blobs from non-osi.


However I am not planning to do that sooner. As a matter of fact, given 
that this is not a blocking issue and given the scope of what still 
needs to be addressed for RPi3 integration, I am kind of hoping we won't 
see an ATF release for another month or two, so that we can get through 
the current integration process, with the current binary blobs in 
non-osi, and then sort out their removal post integration.


Personally, I also expect that anybody who wants to build the binaries 
locally, can simply be directed to follow the official ATF build notes 
on how to set up their tool chain, and then refer to the build 
parameters from our readme. But then again, if we have a nice Docker 
solution to provide, I don't see why we shouldn't also point to it.


On the other hand, when it comes to providing trustworthy links, and 
since there exists a MinGW32 version of the Linaro GCC compilers, I'd 
much rather use AppVeyor for automated build of ATF binaries. The nice 
thing is that AppVeyor can keep and serve its build artefacts, so we'd 
be able to directly link to those, which should give some level of trust 
that the binaries haven't been tampered with by the owner of the repo 
(or at least that, if they have, the source would reflect it). And you 
can also easily configure AppVeyor to only build on tagged commits, 
which I think is what we want.


At any rate, I think there exists more than one solution to address the 
ATF binary provision problem for RPi3, while also not having to ask 
users to blindly trust any binaries we might link to. Maybe Docker or 
AppVeyor are what we want to use. Maybe there is yet another option on 
the table that we haven't talked about yet.


If we believe this is necessary, I can look into adding AppVeyor builds 
of the official ATF ASAP. But for the time being, I would prefer if we 
start discussing this in earnest once ATF 2.1 has been tagged for 
release, and I send a formal proposal to address the removal of the 
non-osi ATF binaries.


Regards,

/Pete

___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH v2 edk2-platforms 16/20] Platform/Broadcom/RPi3: Add Raspberry Pi 3 Platform

2018-12-14 Thread Pete Batard

Hi Ard,

Thanks for the review. I will incorporate the points you raised for 8/9/13.

On 2018.12.14 15:39, Ard Biesheuvel wrote:

On Mon, 10 Dec 2018 at 13:39, Pete Batard  wrote:


Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/Broadcom/Bcm283x/RaspberryPiPkg.dec |  63 ++
  Platform/Broadcom/Bcm283x/RaspberryPiPkg.dsc | 636 
  Platform/Broadcom/Bcm283x/RaspberryPiPkg.fdf | 450 ++
  Platform/Broadcom/Bcm283x/Readme.md  | 263 
  4 files changed, 1412 insertions(+)



This looks fine as well.

In general, please screen the code for the cosmetic stuff I mentioned.
I haven't looked at the SD/MMC drivers yet, and I don't have the
bandwidth (or the energy) to do a deep review of those. The NV storage
driver I will have a more careful look at, but not today - hopefully
by the end of next week,


I tried running PatchCheck.py but it doesn't seem to run on my system
(I have never used it in anger myself), but it should help you spot
many of the issues I brought up.


I ran PatchCheck.py on every single patch sent, and made sure the script 
didn't report any issue.


For instance, this is what PatchCheck reports on my Debian system for 
the 16 patches that apply to edk2-platforms:


-

# python /usr/src/edk2/BaseTools/Scripts/PatchCheck.py -16
Checking git commit: 4695ab7f1bc7
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 4583a4c82bba
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 7406c575c809
The commit message format passed all checks.
The code passed all checks.

Checking git commit: b5ee3fe94aae
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 937845c4cc4d
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 07dae6403e5e
The commit message format passed all checks.
The code passed all checks.

Checking git commit: cef8f008d1fa
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 5304797f8494
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 17711dc8965d
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 83627cdc550f
The commit message format passed all checks.
The code passed all checks.

Checking git commit: c4f87ea8987b
The commit message format passed all checks.
The code passed all checks.

Checking git commit: ca99e77c7b95
The commit message format passed all checks.
The code passed all checks.

Checking git commit: eddd965e2672
The commit message format passed all checks.
The code passed all checks.

Checking git commit: 0893ac6d1509
The commit message format passed all checks.
The code passed all checks.

Checking git commit: f595d0be1e86
The commit message format passed all checks.
The code passed all checks.

Checking git commit: fe4b945ac743
The commit message format passed all checks.
The code passed all checks.

-

So, it doesn't look like the script is current set to detect space 
before parenthesis and the other cosmetic issues you pointed out. But 
I'll make sure to get that fixed. I am currently looking into having the 
Visual Studio editor automate the process of fixing this.


Note that I also found that 'git am' complained about empty newlines 
being present at the end of some files, so I'll fix that too.


Regards,

/Pete
___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH v2 edk2-platforms 07/20] Platform/Broadcom/RPi3: Add Firmware driver

2018-12-13 Thread Pete Batard

On 2018.12.12 21:17, Ard Biesheuvel wrote:

On Mon, 10 Dec 2018 at 13:39, Pete Batard  wrote:


Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/Broadcom/Bcm283x/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c   | 1085 

  Platform/Broadcom/Bcm283x/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.inf |   49 +
  Platform/Broadcom/Bcm283x/Include/Protocol/RaspberryPiFirmware.h|  131 +++
  3 files changed, 1265 insertions(+)



Reviewed-by: Ard Biesheuvel 

Please move this patch forward in the series. It is depended upon by
earlier patches


I'll move the patch forward. Thanks for the reviews.


diff --git a/Platform/Broadcom/Bcm283x/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c 
b/Platform/Broadcom/Bcm283x/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
new file mode 100644
index ..50f3ed3f1e36
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
@@ -0,0 +1,1085 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrei Warkentin 
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include 
+
+//
+// The number of statically allocated buffer pages
+//
+#define NUM_PAGES   1
+
+//
+// The number of iterations to perform when waiting for the mailbox
+// status to change
+//
+#define MAX_TRIES   0x10
+
+STATIC VOID  *mDmaBuffer;
+STATIC VOID  *mDmaBufferMapping;
+STATIC UINTN mDmaBufferBusAddress;
+
+STATIC SPIN_LOCK mMailboxLock;
+
+STATIC
+BOOLEAN
+DrainMailbox (
+  VOID
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if (Val & (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+BOOLEAN
+MailboxWaitForStatusCleared (
+  IN  UINTN   StatusMask
+  )
+{
+  INTNTries;
+  UINT32  Val;
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  Tries = 0;
+  do {
+Val = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_STATUS_OFFSET);
+if ((Val & StatusMask) == 0) {
+  return TRUE;
+}
+ArmDataSynchronizationBarrier ();
+  } while (++Tries < MAX_TRIES);
+
+  return FALSE;
+}
+
+STATIC
+EFI_STATUS
+MailboxTransaction (
+  INUINTN   Length,
+  INUINTN   Channel,
+  OUT   UINT32  *Result
+  )
+{
+  if (Channel >= BCM2836_MBOX_NUM_CHANNELS) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Get rid of stale response data in the mailbox
+  //
+  if (!DrainMailbox ()) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for mailbox to drain\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Wait for the 'output register full' bit to become clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_FULL)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for outbox to become empty\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Start the mailbox transaction
+  //
+  MmioWrite32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_WRITE_OFFSET,
+(UINT32)((UINTN)mDmaBufferBusAddress | Channel));
+
+  ArmDataSynchronizationBarrier ();
+
+  //
+  // Wait for the 'input register empty' bit to clear
+  //
+  if (!MailboxWaitForStatusCleared (1U << BCM2836_MBOX_STATUS_EMPTY)) {
+DEBUG ((DEBUG_ERROR, "%a: timeout waiting for inbox to become full\n",
+  __FUNCTION__));
+return EFI_TIMEOUT;
+  }
+
+  //
+  // Read back the result
+  //
+  ArmDataSynchronizationBarrier ();
+  *Result = MmioRead32 (BCM2836_MBOX_BASE_ADDRESS + BCM2836_MBOX_READ_OFFSET);
+  ArmDataSynchronizationBarrier ();
+
+  return EFI_SUCCESS;
+}
+
+#pragma pack(1)
+typedef struct {
+  UINT32BufferSize;
+  UINT32Response;
+} RPI_FW_BUFFER_HEAD;
+
+typedef struct {
+  UINT32TagId;
+  UINT32TagSize;
+  UINT32TagValueSize;
+} RPI_FW_TAG_HEAD;
+
+typedef struct {
+  UINT32  DeviceId;
+  UINT32  PowerState;
+} RPI_FW_POWER_STATE_TAG;
+
+typedef struct {
+  RPI_FW_BUFFER_HEADBufferHead;
+  RPI_FW_TAG_HEAD   TagHead;
+  R

Re: [edk2] [PATCH v2 edk2-platforms 06/20] Platform/Broadcom/RPi3: Add Interrupt and Device Tree drivers

2018-12-13 Thread Pete Batard

On 2018.12.12 21:09, Ard Biesheuvel wrote:

On Mon, 10 Dec 2018 at 13:39, Pete Batard  wrote:


Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/Broadcom/Bcm283x/Drivers/Bcm2836InterruptDxe/Bcm2836InterruptDxe.c   
| 367 +++
  Platform/Broadcom/Bcm283x/Drivers/Bcm2836InterruptDxe/Bcm2836InterruptDxe.inf 
|  48 +++


These look mostly fine, but please check for cosmetic issues (space
before (, no space after a cast)


Sure.




  Platform/Broadcom/Bcm283x/Drivers/RpiFdtDxe/RpiFdtDxe.c   
| 370 
  Platform/Broadcom/Bcm283x/Drivers/RpiFdtDxe/RpiFdtDxe.inf 
|  53 +++


Some comments below


  4 files changed, 838 insertions(+)

diff --git 
a/Platform/Broadcom/Bcm283x/Drivers/Bcm2836InterruptDxe/Bcm2836InterruptDxe.c 
b/Platform/Broadcom/Bcm283x/Drivers/Bcm2836InterruptDxe/Bcm2836InterruptDxe.c
new file mode 100644
index ..dda61665031d
--- /dev/null
+++ 
b/Platform/Broadcom/Bcm283x/Drivers/Bcm2836InterruptDxe/Bcm2836InterruptDxe.c
@@ -0,0 +1,367 @@
+/** @file
+ *
+ *  Copyright (c) 2016, Linaro, Ltd. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+//
+// This currently only implements support for the architected timer interrupts
+// on the per-CPU interrupt controllers.
+//
+#define NUM_IRQS(4)
+
+#ifdef MDE_CPU_AARCH64
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_AARCH64_IRQ
+#else
+#define ARM_ARCH_EXCEPTION_IRQ  EXCEPT_ARM_IRQ
+#endif
+
+STATIC CONST
+EFI_PHYSICAL_ADDRESS RegBase = FixedPcdGet32 (PcdInterruptBaseAddress);
+
+//
+// Notifications
+//
+STATIC EFI_EVENTmExitBootServicesEvent;
+STATIC HARDWARE_INTERRUPT_HANDLER   mRegisteredInterruptHandlers[NUM_IRQS];
+
+/**
+  Shutdown our hardware
+
+  DXE Core will disable interrupts and turn off the timer and disable 
interrupts
+  after all the event handlers have run.
+
+  @param[in]  Event   The Event that is being processed
+  @param[in]  Context Event Context
+**/
+STATIC
+VOID
+EFIAPI
+ExitBootServicesEvent (
+  IN EFI_EVENT  Event,
+  IN VOID   *Context
+  )
+{
+  // Disable all interrupts
+  MmioWrite32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 0);
+}
+
+/**
+  Enable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt enabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+EnableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT(FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioOr32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, 1 << Source);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Disable interrupt source Source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+
+  @retval EFI_SUCCESS   Source interrupt disabled.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+DisableInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT(FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  MmioAnd32 (RegBase + BCM2836_INTC_TIMER_CONTROL_OFFSET, ~(1 << Source));
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Register Handler for the specified interrupt source.
+
+  @param This Instance pointer for this protocol
+  @param Source   Hardware source of the interrupt
+  @param Handler  Callback for interrupt. NULL to unregister
+
+  @retval EFI_SUCCESS Source was updated to support Handler.
+  @retval EFI_DEVICE_ERROR  Hardware could not be programmed.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+RegisterInterruptSource (
+  IN EFI_HARDWARE_INTERRUPT_PROTOCOL*This,
+  IN HARDWARE_INTERRUPT_SOURCE  Source,
+  IN HARDWARE_INTERRUPT_HANDLER Handler
+  )
+{
+  if (Source >= NUM_IRQS) {
+ASSERT (FALSE);
+return EFI_UNSUPPORTED;
+  }
+
+  if (Handler == NULL && mRegisteredInterruptHandlers[Source] == NULL) {
+return EFI_INVALID_PARAMETER;
+  }
+
+  if (Handler != NULL && mRegisteredInterruptHandlers[Source] != NULL) {
+return EFI_ALREADY_STARTED;
+  }
+
+  mRegisteredInterruptHandlers[Source] 

Re: [edk2] [PATCH v2 edk2-platforms 04/20] Platform/Broadcom/RPi3: Add ACPI Tables

2018-12-13 Thread Pete Batard

On 2018.12.12 20:52, Ard Biesheuvel wrote:

On Mon, 10 Dec 2018 at 13:39, Pete Batard  wrote:


Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/Broadcom/Bcm283x/AcpiTables/AcpiTables.inf |  51 ++
  Platform/Broadcom/Bcm283x/AcpiTables/Csrt.aslc  | 337 +
  Platform/Broadcom/Bcm283x/AcpiTables/Dbg2.aslc  |  32 ++
  Platform/Broadcom/Bcm283x/AcpiTables/Dsdt.asl   | 523 
  Platform/Broadcom/Bcm283x/AcpiTables/Fadt.aslc  |  50 ++
  Platform/Broadcom/Bcm283x/AcpiTables/Gtdt.aslc  |  31 ++
  Platform/Broadcom/Bcm283x/AcpiTables/Madt.aslc  |  60 +++
  Platform/Broadcom/Bcm283x/AcpiTables/Pep.asl|  92 
  Platform/Broadcom/Bcm283x/AcpiTables/Pep.c  |  84 
  Platform/Broadcom/Bcm283x/AcpiTables/Pep.h  | 126 +
  Platform/Broadcom/Bcm283x/AcpiTables/Platform.h |  82 +++
  Platform/Broadcom/Bcm283x/AcpiTables/Rhpx.asl   | 201 
  Platform/Broadcom/Bcm283x/AcpiTables/Sdhc.asl   | 105 
  Platform/Broadcom/Bcm283x/AcpiTables/Spcr.asl   |  53 ++
  Platform/Broadcom/Bcm283x/AcpiTables/Uart.asl   | 155 ++
  15 files changed, 1982 insertions(+)



I'm not crazt about the hexdump-style files in this patch but I can
live with it.

One remark below.


diff --git a/Platform/Broadcom/Bcm283x/AcpiTables/AcpiTables.inf 
b/Platform/Broadcom/Bcm283x/AcpiTables/AcpiTables.inf
new file mode 100644
index ..db0270270cf9
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/AcpiTables/AcpiTables.inf
@@ -0,0 +1,51 @@
+#/** @file
+#
+#  ACPI table data and ASL sources required to boot the platform.
+#
+#  Copyright (c) 2017, Andrey Warkentin 
+#  Copyright (c) Microsoft Corporation. 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.
+#
+#**/
+
+[Defines]
+  INF_VERSION= 0x00010005


I'll mention it here but it applies everywhere: please use 0x0001001A
as the INF version.
(In general, use the latest published version of each file type)


Good point. We'll fix that.


+  BASE_NAME  = AcpiTables
+  FILE_GUID  = 7E374E25-8E01-4FEE-87F2-390C23C606CD
+  MODULE_TYPE= USER_DEFINED
+  VERSION_STRING = 1.0
+
+#
+# The following information is for reference only and not required by the 
build tools.
+#
+#  VALID_ARCHITECTURES   = AARCH64
+#
+
+[Sources]
+  Platform.h
+  Madt.aslc
+  Fadt.aslc
+  Dbg2.aslc
+  Gtdt.aslc
+  Dsdt.asl
+  Csrt.aslc
+  Spcr.asl
+
+[Packages]
+  MdePkg/MdePkg.dec
+  EmbeddedPkg/EmbeddedPkg.dec
+
+[FixedPcd]
+  gEmbeddedTokenSpaceGuid.PcdInterruptBaseAddress
+
+[BuildOptions]
+  # The default '-mcmodel=small' used with DEBUG produces a GenFw error when 
compiling CSRT.acpi:
+  # "AARCH64 small code model requires identical ELF and PE/COFF section offsets 
modulo 4 KB."
+  GCC:DEBUG_*_AARCH64_CC_FLAGS = -mcmodel=tiny
diff --git a/Platform/Broadcom/Bcm283x/AcpiTables/Csrt.aslc 
b/Platform/Broadcom/Bcm283x/AcpiTables/Csrt.aslc
new file mode 100644
index ..926942ec8da3
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/AcpiTables/Csrt.aslc
@@ -0,0 +1,337 @@
+/** @file
+ *
+ *  Core System Resource Table (CSRT)
+ *
+ *  Copyright (c) Microsoft Corporation. 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 "Platform.h"
+
+#define DMA_MAX_REQ_LINES 32
+
+#pragma pack(push, 1)
+
+//
+// DMA Controller Vendor Data for RPi3
+//
+typedef struct
+{
+UINT32 Length;
+UINT32 Type;
+UINT64 ChannelsBaseAddress;
+UINT32 ChannelsBaseSize;
+UINT64 ControllerBaseAddress;
+UINT32 ControllerBaseSize;
+UINT32 ChannelCount;
+UINT32 ControllerInterrupt;
+UINT32 MinimumRequestLine;
+UINT32 MaximumRequestLine;
+BOOLEAN CacheCoherent;
+} DMA_CONTROLLER_VENDOR_DATA;
+
+//
+// DMA Controller on RPi3
+//

Re: [edk2] [PATCH v2 edk2-platforms 03/20] Platform/Broadcom/RPi3: Add GPIO and RTC libraries

2018-12-13 Thread Pete Batard

On 2018.12.12 20:50, Ard Biesheuvel wrote:

On Mon, 10 Dec 2018 at 13:39, Pete Batard  wrote:


Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2837Gpio.h  
|  50 +
  Platform/Broadcom/Bcm283x/Include/Library/GpioLib.h   
|  33 +++
  Platform/Broadcom/Bcm283x/Include/Utils.h 
|  33 +++
  Platform/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c   
|  79 +++
  Platform/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf 
|  39 
  
Platform/Broadcom/Bcm283x/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.c
   | 222 
  
Platform/Broadcom/Bcm283x/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
 |  43 
  7 files changed, 499 insertions(+)

diff --git a/Platform/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2837Gpio.h 
b/Platform/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2837Gpio.h
new file mode 100644
index ..27e6665f1745
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2837Gpio.h
@@ -0,0 +1,50 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) Microsoft Corporation. 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.
+ *
+ **/
+
+#ifndef __BCM2837GPIO_H__
+#define __BCM2837GPIO_H__
+
+#define GPIO_BASE_ADDRESS  (BCM2836_SOC_REGISTERS + 0x0020)
+
+#define GPIO_GPFSEL0   (GPIO_BASE_ADDRESS + 0x00)
+#define GPIO_GPFSEL1   (GPIO_BASE_ADDRESS + 0x04)
+#define GPIO_GPFSEL2   (GPIO_BASE_ADDRESS + 0x08)
+#define GPIO_GPFSEL3   (GPIO_BASE_ADDRESS + 0x0C)
+#define GPIO_GPFSEL4   (GPIO_BASE_ADDRESS + 0x10)
+#define GPIO_GPFSEL5   (GPIO_BASE_ADDRESS + 0x14)
+
+#define GPIO_GPCLR0(GPIO_BASE_ADDRESS + 0x28)
+#define GPIO_GPCLR1(GPIO_BASE_ADDRESS + 0x2C)
+
+#define GPIO_GPSET0(GPIO_BASE_ADDRESS + 0x1C)
+#define GPIO_GPSET1(GPIO_BASE_ADDRESS + 0x20)
+
+#define GPIO_FSEL_INPUT0x0
+#define GPIO_FSEL_OUTPUT   0x1
+#define GPIO_FSEL_ALT0 0x4
+#define GPIO_FSEL_ALT1 0x5
+#define GPIO_FSEL_ALT2 0x6
+#define GPIO_FSEL_ALT3 0x7
+#define GPIO_FSEL_ALT4 0x3
+#define GPIO_FSEL_ALT5 0x2
+
+#define GPIO_FSEL_PINS_PER_REGISTER 10
+#define GPIO_FSEL_BITS_PER_PIN  3
+#define GPIO_FSEL_MASK  ((1 << GPIO_FSEL_BITS_PER_PIN) - 1)
+
+#define GPIO_PINS  54
+
+#endif // __BCM2837GPIO_H__
+
diff --git a/Platform/Broadcom/Bcm283x/Include/Library/GpioLib.h 
b/Platform/Broadcom/Bcm283x/Include/Library/GpioLib.h
new file mode 100644
index ..ca9da4b11a87
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Include/Library/GpioLib.h
@@ -0,0 +1,33 @@
+/** @file
+ *
+ *  GPIO manipulation.
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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.
+ *
+ **/
+
+#ifndef __GPIO_LIB__
+#define __GPIO_LIB__
+
+#include 
+
+VOID
+GpioPinFuncSet(


Space before ( please [throughout]


Okay. I'll spend some time going over our files to make sure the 
formatting is consistent with the EDK2 coding style.



+  IN  UINTN Pin,
+  IN  UINTN Function
+  );
+
+UINTN
+GpioPinFuncGet(
+  IN  UINTN Pin
+  );
+
+#endif /* __GPIO_LIB__ */
diff --git a/Platform/Broadcom/Bcm283x/Include/Utils.h 
b/Platform/Broadcom/Bcm283x/Include/Utils.h
new file mode 100644
index ..47713275de3f
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Include/Utils.h
@@ -0,0 +1,33 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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.
+ *
+ **/
+
+#ifndef UTILS_H
+#define UTILS_H
+
+#define _IX_BITS(sm

Re: [edk2] [PATCH v2 edk2-platforms 01/20] Platform/Broadcom/RPi3: Add Reset and Memory Init libraries

2018-12-13 Thread Pete Batard

On 2018.12.12 20:43, Ard Biesheuvel wrote:

On Mon, 10 Dec 2018 at 13:39, Pete Batard  wrote:


Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
  Platform/Broadcom/Bcm283x/Library/MemoryInitPeiLib/MemoryInitPeiLib.c   | 183 

  Platform/Broadcom/Bcm283x/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf |  51 
++
  Platform/Broadcom/Bcm283x/Library/ResetLib/ResetLib.c   | 104 
+++
  Platform/Broadcom/Bcm283x/Library/ResetLib/ResetLib.inf |  46 
+
  4 files changed, 384 insertions(+)

diff --git 
a/Platform/Broadcom/Bcm283x/Library/MemoryInitPeiLib/MemoryInitPeiLib.c 
b/Platform/Broadcom/Bcm283x/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
new file mode 100644
index ..81d810b5d428
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
@@ -0,0 +1,183 @@
+/** @file
+ *
+ *  Copyright (c) 2017-2018, Andrey Warkentin 
+ *  Copyright (c) 2011-2015, ARM Limited. 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+extern UINT64 mSystemMemoryEnd;
+
+VOID
+BuildMemoryTypeInformationHob (
+  VOID
+  );
+
+STATIC
+VOID
+InitMmu (
+  IN ARM_MEMORY_REGION_DESCRIPTOR  *MemoryTable
+  )
+{
+
+  VOID  *TranslationTableBase;
+  UINTN TranslationTableSize;


You can drop these


+  RETURN_STATUS Status;
+
+  //Note: Because we called PeiServicesInstallPeiMemory() before to call 
InitMmu() the MMU Page Table resides in
+  //  DRAM (even at the top of DRAM as it is the first permanent memory 
allocation)
+  Status = ArmConfigureMmu (MemoryTable, &TranslationTableBase, 
&TranslationTableSize);


... given that they are only passed here, and are actually OPTIONAL


Good point. We'll drop these and pass VOID instead.


+  if (EFI_ERROR (Status)) {
+DEBUG ((DEBUG_ERROR, "Error: Failed to enable MMU\n"));
+  }
+}
+
+STATIC
+VOID
+AddAndRTSData(ARM_MEMORY_REGION_DESCRIPTOR *Desc)


What does AddAnd mean?


I'm not sure myself, so I agree this is confusing and needs to be changed.


Can we improve the naming in the file?


We'll rename all the AddAnd... function calls to something that is more 
explicit wrt the goal of these functions.



Also, please use a space before (


Okay.


+{
+  BuildResourceDescriptorHob (
+  EFI_RESOURCE_SYSTEM_MEMORY,
+  EFI_RESOURCE_ATTRIBUTE_PRESENT |
+  EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+  EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+  EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+  EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+  EFI_RESOURCE_ATTRIBUTE_TESTED,
+  Desc->PhysicalBase,
+  Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiRuntimeServicesData
+);
+}
+
+STATIC
+VOID
+AddAndReserved(ARM_MEMORY_REGION_DESCRIPTOR *Desc)
+{
+  BuildResourceDescriptorHob (
+  EFI_RESOURCE_SYSTEM_MEMORY,
+  EFI_RESOURCE_ATTRIBUTE_PRESENT |
+  EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
+  EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
+  EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
+  EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
+  EFI_RESOURCE_ATTRIBUTE_TESTED,
+  Desc->PhysicalBase,
+  Desc->Length
+  );
+
+  BuildMemoryAllocationHob (
+Desc->PhysicalBase,
+Desc->Length,
+EfiReservedMemoryType
+);
+}
+
+STATIC
+VOID
+AddAndMmio(ARM_MEMORY_REGION_DESCRIPTOR *Desc)
+{
+  BuildResourceDescriptorHob (
+  EFI_RESOURCE_SYSTEM_MEMORY,
+  (EFI_RESOURCE_ATTRIBUTE_PRESENT|
+   EFI_RESOURCE_ATTRIBUTE_INITIALIZED |

Re: [edk2] [PATCH v2 edk2-platforms 00/20] Platform/Broadcom: Add Raspberry Pi 3 support

2018-12-12 Thread Pete Batard

On 2018.12.12 18:32, Leif Lindholm wrote:

On Tue, Dec 11, 2018 at 08:16:07PM +, Pete Batard wrote:

I _think_ all of the ATF binaries we have in non-osi are
non-upstream. If the port for the rpi3 is upstream, I would be just as
happy to have simple build instructions of a known good commit (with
notes on toolchain version tested) in the Readme.md - and possibly a
placeholder directory with a .inf in to drop a prebuilt image into.


Well, while it is upstream, it is built using a custom rpi3 specific option
which we had ATF to add, so that we could get a memory mapping that works
with Windows (default one was okay for Linux but not Windows). So I doubt we
will ever get upstream binaries that we can use as is, if that's what you
are alluding to.


I don't really care all that much about that, but if we need
modifications to run Windows (and nothing prevents that from working
with Linux), then should we try to change the upstream defaults?


The problem here is U-Boot.

The RPi3 ATF has pretty much been designed around U-Boot usage (since it 
was its only consumer until now), so, to achieve the above, we'll need 
to validate that we aren't going to break anything for downstream 
RPi3/U-Boot users, which I don't think we can accomplish in a reasonable 
timeframe.


When I mentioned that the ATF works with Linux and Windows, I meant 
"when used as part of an UEFI payload". But I have yet to run any 
comprehensive test of our UEFI-tailored ATF within a U-Boot payload. And 
even though nobody should be relying on a fixed memory mapping, I do 
some have concerns that some people might have downstream elements that 
are tailored around the current U-Boot friendly memory mapping, which we 
might break.


As such, I'd rather first see usage of the UEFI bootloader take off with 
a few Linux distros, after it has been officialized in edk2-platforms, 
so that we can have a bit more weight in asking the U-Boot folks whether 
they'd consider using our memory mapping as default.



But for me, I'd be happy with just the build instructions you have,
and no binaries/license, in
edk2-platforms/Platform/RaspberryPi/RPi3/Arm-Tf/, with the user having
to drop in their own binaries.


In that case, I think I'd still like to provide some links to 
downloadable binaries, for people who don't want to have to rebuild ATF, 
and who consider that download links provided in an official readme 
should be trustworthy enough.


I can certainly upload binary releases for the required ATF files in my 
github clone of ATF, and then add links to that in the readme with the 
instructions on how to rebuild ATF.


However, I'd rather wait to do that until there has been an official 
tagged new release for ATF (which would be 2.1 at this stage, since our 
changes have been applied after 2.0), as it'll look better for Pi users 
to see an initial ATF serial output that says "BL<#>: v2.1(release)" 
instead of the current "BL<#>: v2.0(release):v2.0-278-gc3859557"


How about this then: If ATF produces a formal release before this 
proposal is integrated, I'll amend it to remove the ATF binary blobs and 
apply the suggestion above, with the Atf/ build/link data moved out of 
non-osi. But if they don't, I'll keep the proposal for ATF as is, and 
then submit a patch to remove the non-osi data and apply the above at a 
later date, once there has been a new ATF release.


Regards,

/Pete
___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH v2 edk2-platforms 00/20] Platform/Broadcom: Add Raspberry Pi 3 support

2018-12-11 Thread Pete Batard

Hi Leif,

On 2018.12.11 18:10, Leif Lindholm wrote:

Hi Pete,

Many thanks for this.
I expect Ard will give more detailed review, since he has some
familiarity with the port. But I wanted to make a few comments.

Could you cc me as well on any future revisions?


Will do.


On Mon, Dec 10, 2018 at 12:38:33PM +, Pete Batard wrote:

Version History:

* v2: Break down the content into logical entities of more manageable size.
   Please pay attention to the *NON-OSI* tagged patches, that should be
   applied to edk2-non-osi instead of edk2-platforms.

Preamble:

Because of its price point, ease of use and availability, the Raspberry Pi is
undeniably one of the most successful ARM platform in existence today. Its
widespread adoption therefore makes it a perfect fit as an EDK2 platform.

However, up until now, the Raspberry Pi hasn't been supported as a bona fide
platform in our repository. This series of patches remedies that by introducing
the Raspberry Pi 3 Model B and Model B+ as a viable EDK2 platforms.

With regards to the latter:
* Even though the ARM Trusted Firmware binary blobs are subject to a
   BSD-3-Clause licence, which may be compatible with the EDK2 one, we chose
   to follow the lead of other platforms that provide ATF binaries in non OSI.


I _think_ all of the ATF binaries we have in non-osi are
non-upstream. If the port for the rpi3 is upstream, I would be just as
happy to have simple build instructions of a known good commit (with
notes on toolchain version tested) in the Readme.md - and possibly a
placeholder directory with a .inf in to drop a prebuilt image into.


Well, while it is upstream, it is built using a custom rpi3 specific 
option which we had ATF to add, so that we could get a memory mapping 
that works with Windows (default one was okay for Linux but not 
Windows). So I doubt we will ever get upstream binaries that we can use 
as is, if that's what you are alluding to.


For the record, there's a readme located in the directory where the ATF 
binaries are provided, that has the full build command we used, as well 
as the description of the memory mapping.



(If it isn't upstream, non-osi is the way to go for now.)
((This isn't a "do what I say", this is a "you don't have to".))


I guess that means we'll keep the binaries in non-osi for now then.

However, I have been thinking about renaming the directory that contains 
the ATF blobs from "Binary/" to "Atf/" to make it more explicit. I'll 
probably do for the v3, unless someone has a different idea.



* The Device Tree binaries (and source descriptors) are subject to a GPLv2
   license, as per the ones published by the Raspberry Pi Foundation.


This feels somewhat suboptimal. Is there someone you could ask whether
they'd be willing to dual license?


As per the official Raspberry Pi firmware repo at 
https://github.com/raspberrypi/firmware:


  The dtbs, overlays and associated README are built from Linux kernel
  sources, released under the GPL (see boot/COPYING.linux)

The part about it being built from kernel sources makes me think that, 
unless we get all the Linux developers who touched on the relevant parts 
of the Device Tree to agree on dual licensing, which would be a 
tremendous effort, it's going to have to remain GPLv2.


As a result, even if it wanted to, I doubt the Raspberry Pi foundation 
would have the authority to dual license the .dtb's/.dts's. But if you 
see it differently, I can try to get in touch with the Foundation.



By the way, since I have just seen from Ard's recent OverdriveBoard 
patches that we do have the ability to compile a Device Tree from source 
(which I wasn't aware of), I'm going to point out that we probably don't 
want to do that for this platform.


The reason is: we need to make it easy for users of the Pi3 Model B+ to 
override the default Model B Device Tree, which is what we embed in the 
firmware (since it's the most compatible one), and of course it'll be a 
lot more annoying for people to do that all they have is the .dts. And 
we can't refer users to the official Pi Foundation .dtb's, as they 
produce USB keyboard issues and are missing some of the features we want.


Moreover, even if we were to decide that we'd like to have at least the 
Model B Device Tree built from source, we'd still also need to provide 
the .dtb for that one as, if you want to use one of the many DT overlays 
provided by the Pi Foundation (see 
https://github.com/raspberrypi/firmware/tree/master/boot/overlays) then 
you have no choice but to also provide the base .dtb at the same time, 
which means that Rpi3 users will want to have easy access to a 
precompiled custom bcm2710-rpi-3-b.dtb as well.



If not, this is certainly yet another argument for an edk2-non-bsd
repository or suchlike.


* The DwUsbHostDxe driver is subject to a GPLv2 

[edk2] [PATCH v2 edk2-platforms 19/20] Platform/Broadcom/RPi3 *NON-OSI*: Add USB Host driver

2018-12-10 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/ComponentName.c  |  219 +++
 Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/DriverBinding.c  |  269 
 Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/DwUsbHostDxe.c   | 1644 

 Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/DwUsbHostDxe.h   |  159 ++
 Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/DwUsbHostDxe.inf |   52 +
 Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/DwcHw.h  |  782 
++
 Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/License.txt  |  340 
 7 files changed, 3465 insertions(+)

diff --git a/Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/ComponentName.c 
b/Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/ComponentName.c
new file mode 100644
index ..6e2709b13c9b
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Drivers/DwUsbHostDxe/ComponentName.c
@@ -0,0 +1,219 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrey Warkentin 
+ *
+ *  SPDX-License-Identifier: GPL-2.0+
+ *
+ **/
+
+#include "DwUsbHostDxe.h"
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+);
+
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetControllerName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL *This,
+  IN  EFI_HANDLE  ControllerHandle,
+  IN  EFI_HANDLE  ChildHandle,
+  IN  CHAR8   *Language,
+  OUT CHAR16  **ControllerName
+  );
+
+//
+// EFI Component Name Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
+  ComponentNameGetDriverName,
+  ComponentNameGetControllerName,
+  "eng"
+};
+
+//
+// EFI Component Name 2 Protocol
+//
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
+  (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) ComponentNameGetDriverName,
+  (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) ComponentNameGetControllerName,
+  "en"
+};
+
+
+STATIC EFI_UNICODE_STRING_TABLE mDriverName[] = {
+  {
+"eng;en",
+(CHAR16 *)L"Raspberry Pi USB Host Driver"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mDeviceName[] = {
+  {
+"eng;en",
+(CHAR16 *)L"Raspberry Pi USB Host"
+  },
+  {
+NULL,
+NULL
+  }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]  A pointer to the EFI_COMPONENT_NAME2_PROTOCOL 
or
+EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]  A pointer to a Null-terminated ASCII string
+array indicating the language. This is the
+language of the driver name that the caller is
+requesting, and it must match one of the
+languages specified in SupportedLanguages. The
+number of languages supported by a driver is up
+to the driver writer. Language is specified
+in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]   A pointer to the Unicode string to return.
+This Unicode string is the name of the
+driver specified by This in the language
+specified by Language.
+
+  @retval EFI_SUCCESS   The Unicode string for the Driver specified by
+This and the language specified by Language was
+returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED   The driver specified by This does not support
+the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+ComponentNameGetDriverName (
+  IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
+  IN  CHAR8*Language,
+  OUT CHAR16   **DriverName
+  )
+{
+  return LookupUnicodeString2 (
+   Language,
+   This->SupportedLanguages,
+   mDriverName,
+   DriverName,
+   (BOOLEAN)(This == &gComponentName)
+   );
+}
+
+/**
+  Retrieves a Uni

[edk2] [PATCH v2 edk2-platforms 18/20] Platform/Broadcom/RPi3 *NON-OSI*: Add Device Tree binaries

2018-12-10 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Broadcom/Bcm283x/DeviceTree/License.txt  |  340 ++
 Platform/Broadcom/Bcm283x/DeviceTree/bcm2710-rpi-3-b-plus.dtb |  Bin 0 -> 
25617 bytes
 Platform/Broadcom/Bcm283x/DeviceTree/bcm2710-rpi-3-b-plus.dts | 1263 

 Platform/Broadcom/Bcm283x/DeviceTree/bcm2710-rpi-3-b.dtb  |  Bin 0 -> 
25354 bytes
 Platform/Broadcom/Bcm283x/DeviceTree/bcm2710-rpi-3-b.dts  | 1259 
+++
 5 files changed, 2862 insertions(+)

diff --git a/Platform/Broadcom/Bcm283x/DeviceTree/License.txt 
b/Platform/Broadcom/Bcm283x/DeviceTree/License.txt
new file mode 100644
index ..1603937dad82
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/DeviceTree/License.txt
@@ -0,0 +1,340 @@
+GNU GENERAL PUBLIC LICENSE
+   Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+

[edk2] [PATCH v2 edk2-platforms 17/20] Platform/Broadcom/RPi3 *NON-OSI*: Add ATF binaries

2018-12-10 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Broadcom/Bcm283x/Binary/License.txt |  26 +
 Platform/Broadcom/Bcm283x/Binary/README.md   |  41 
 Platform/Broadcom/Bcm283x/Binary/bl1.bin | Bin 0 -> 18801 bytes
 Platform/Broadcom/Bcm283x/Binary/fip.bin | Bin 0 -> 41714 bytes
 4 files changed, 67 insertions(+)

diff --git a/Platform/Broadcom/Bcm283x/Binary/License.txt 
b/Platform/Broadcom/Bcm283x/Binary/License.txt
new file mode 100644
index ..b98dc643227e
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Binary/License.txt
@@ -0,0 +1,26 @@
+Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without 
modification,
+are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice, 
this
+  list of conditions and the following disclaimer in the documentation and/or
+  other materials provided with the distribution.
+
+* Neither the name of ARM nor the names of its contributors may be used to
+  endorse or promote products derived from this software without specific prior
+  written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Platform/Broadcom/Bcm283x/Binary/README.md 
b/Platform/Broadcom/Bcm283x/Binary/README.md
new file mode 100644
index ..9811a1286093
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Binary/README.md
@@ -0,0 +1,41 @@
+ARM Trusted Firmware for Raspberry Pi 3
+===
+
+The `bl1` and `fip` ATF binaries, found in this directory, were built from
+the [latest ATF](https://github.com/ARM-software/arm-trusted-firmware)
+(commit c3859557) using Linaro's GCC 5.5 compiler with:
+
+```
+make PLAT=rpi3 PRELOADED_BL33_BASE=0x3 RPI3_PRELOADED_DTB_BASE=0x1 
SUPPORT_VFP=1 RPI3_USE_UEFI_MAP=1 fip all
+```
+
+This results in the following memory mapping:
+
+```
+0x +-+
+   |   ROM   | BL1
+0x0001 +-+
+   |   DTB   | (Loaded by the VideoCore)
+0x0002 +-+
+   |   FIP   |
+0x0003 +-+
+   | |
+   |  UEFI PAYLOAD   |
+   | |
+0x0020 +-+
+   |   Secure SRAM   | BL2, BL31
+0x0030 +-+
+   |   Secure DRAM   | BL32 (Secure payload)
+0x0040 +-+
+   | |
+   | |
+   | Non-secure DRAM | BL33
+   | |
+   | |
+0x0100 +-+
+   | |
+   |   ...   |
+   | |
+0x3F00 +-+
+   |   I/O   |
+```
diff --git a/Platform/Broadcom/Bcm283x/Binary/bl1.bin 
b/Platform/Broadcom/Bcm283x/Binary/bl1.bin
new file mode 100644
index 
..e25138828d0a4ddb24772abb1a60eefc334666b5
GIT binary patch
literal 18801
zcmeHud2|%#mG7;pmedFVtpy|tJ-S*z4DF582<)+{TLO%YF^IMyw%@2*ivhC;Enpe2
zRTADp&Ya2NoOkF$@l}1x-M+hhw@Q@U820D96C>N0$f-gpvNVO-6r%k^_LFjhV53f>
zzYYB^p(#wZhayBm(40?m)v%ZRF-jkU{?@RJdpUDVq_0@Qvl>>=KM8BSMAN6D9c2X^
zv}!gTC9-IZXdThl_aE5oz6A)Ol0}J=O^M4yanPmrUEBE6z%_}qi$f`XQKFzu
zlVZ_?G$rnX41{DgYzORyWMj?6p-TMP#*g+gylWdj3`8X%sIOlv1lmB)R$NV@Mf2q+
zY4d>}JWrcSYDNxGR%4SA-?^DI?>kETG0>;<^-cF@D)C*r!0X4PiJ}^Q9sQugsR%T^
zW2zF8L8m(ZKusahN`})&mx1t%~~5>0PQR^krEJ6wr3iAo6aZSmWc_!Ag6
z`56X&xUWmf4DJH$r=?NBrxUaj_-e#D|7a!d7m4LPR23=2^S_NbBGxi5Ce4?hcK6f$
z3TXVu>I#)XZeBsnU^%~vzTYcJS`*qLzY-q@C2Lj%?d)EP7gLhfKy+cZh2moYa~$X{
zx3%XUwhAHXdf#-#Z|vK6?76XLKQ{Io`^9@z>$YethdSW8QZ5C&;8{(g13fpB7t2Z#
zv}wTee+aU&&~*P;CC>ZC@)`!-q9RS70s5}E1#NvkX*|Abte|y4K4Ov@&cGb(htrS!
z?UHhWQSQh3M%HhErgcf#{e7FzN8o|+H4LXX!y1D17Ravw@_ZjU`DI^Uc2

[edk2] [PATCH v2 edk2-platforms 16/20] Platform/Broadcom/RPi3: Add Raspberry Pi 3 Platform

2018-12-10 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Broadcom/Bcm283x/RaspberryPiPkg.dec |  63 ++
 Platform/Broadcom/Bcm283x/RaspberryPiPkg.dsc | 636 
 Platform/Broadcom/Bcm283x/RaspberryPiPkg.fdf | 450 ++
 Platform/Broadcom/Bcm283x/Readme.md  | 263 
 4 files changed, 1412 insertions(+)

diff --git a/Platform/Broadcom/Bcm283x/RaspberryPiPkg.dec 
b/Platform/Broadcom/Bcm283x/RaspberryPiPkg.dec
new file mode 100644
index ..51f2f07aab8a
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/RaspberryPiPkg.dec
@@ -0,0 +1,63 @@
+## @file
+#
+#  Copyright (c) 2016, Linaro, Ltd. All rights reserved.
+#  Copyright (c) 2017 - 2018, Andrei Warkentin 
+#
+#  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.
+#
+##
+
+[Defines]
+  DEC_SPECIFICATION  = 0x0001001A
+  PACKAGE_NAME   = RaspberryPiPkg
+  PACKAGE_GUID   = DFA0CA8B-F3AC-4607-96AC-46FA04B84DCC
+  PACKAGE_VERSION= 0.1
+
+[Includes]
+  Include
+
+[Protocols]
+  gRaspberryPiFirmwareProtocolGuid = { 0x0ACA9535, 0x7AD0, 0x4286, { 0xB0, 
0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiConfigAppliedProtocolGuid = { 0x0ACA, 0x7AD0, 0x4286, { 
0xB0, 0x2E, 0x87, 0xFA, 0x7E, 0x2A, 0x57, 0x11 } }
+  gRaspberryPiMmcHostProtocolGuid = { 0x3e591c00, 0x9e4a, 0x11df, {0x92, 0x44, 
0x00, 0x02, 0xA5, 0xF5, 0xF5, 0x1B } }
+  gExtendedTextOutputProtocolGuid = { 0x387477ff, 0xffc7, 0xffd2, {0x8e, 0x39, 
0x0, 0xff, 0xc9, 0x69, 0x72, 0x3b } }
+
+[Guids]
+  gRaspberryPiTokenSpaceGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+  gRaspberryPiFdtFileGuid = {0xDF5DA223, 0x1D27, 0x47C3, { 0x8D, 0x1B, 0x9A, 
0x41, 0xB5, 0x5A, 0x18, 0xBC}}
+  gRaspberryPiEventResetGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 
0xB4, 0xB4, 0xE4, 0xD4, 0xB4}}
+  gConfigDxeFormSetGuid = {0xCD7CC258, 0x31DB, 0x22E6, {0x9F, 0x22, 0x63, 
0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+
+[PcdsFixedAtBuild.common]
+  gRaspberryPiTokenSpaceGuid.PcdFdtBaseAddress|0x1|UINT32|0x0001
+  gRaspberryPiTokenSpaceGuid.PcdFirmwareBlockSize|0x0|UINT32|0x0002
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogBase|0x0|UINT32|0x0003
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageEventLogSize|0x0|UINT32|0x0004
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageVariableBase|0x0|UINT32|0x0005
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwSpareBase|0x0|UINT32|0x0006
+  gRaspberryPiTokenSpaceGuid.PcdNvStorageFtwWorkingBase|0x0|UINT32|0x0007
+  gRaspberryPiTokenSpaceGuid.PcdBootEpochSeconds|0x0|UINT64|0x0008
+
+[PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx]
+  gRaspberryPiTokenSpaceGuid.PcdHypEnable|0|UINT32|0x0009
+  gRaspberryPiTokenSpaceGuid.PcdHypLogMask|0|UINT32|0x000a
+  gRaspberryPiTokenSpaceGuid.PcdHypWindowsDebugHook|0|UINT32|0x000b
+  gRaspberryPiTokenSpaceGuid.PcdHypWin2000Mask|0|UINT32|0x000c
+  gRaspberryPiTokenSpaceGuid.PcdCpuClock|0|UINT32|0x000d
+  gRaspberryPiTokenSpaceGuid.PcdSdIsArasan|0|UINT32|0x000e
+  gRaspberryPiTokenSpaceGuid.PcdMmcForce1Bit|0|UINT32|0x000f
+  gRaspberryPiTokenSpaceGuid.PcdMmcForceDefaultSpeed|0|UINT32|0x0010
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdDefaultSpeedMHz|0|UINT32|0x0011
+  gRaspberryPiTokenSpaceGuid.PcdMmcSdHighSpeedMHz|0|UINT32|0x0012
+  gRaspberryPiTokenSpaceGuid.PcdMmcDisableMulti|0|UINT32|0x0013
+  gRaspberryPiTokenSpaceGuid.PcdDebugEnableJTAG|0|UINT32|0x0014
+  gRaspberryPiTokenSpaceGuid.PcdDebugShowUEFIExit|0|UINT32|0x0015
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableVModes|0|UINT32|0x0017
+  gRaspberryPiTokenSpaceGuid.PcdDisplayEnableSShot|0|UINT32|0x0018
diff --git a/Platform/Broadcom/Bcm283x/RaspberryPiPkg.dsc 
b/Platform/Broadcom/Bcm283x/RaspberryPiPkg.dsc
new file mode 100644
index ..8610fae0b92f
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/RaspberryPiPkg.dsc
@@ -0,0 +1,636 @@
+# @file
+#
+#  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
+#  Copyright (c) 2014, Linaro Limited. All rights reserved.
+#  Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
+#  Copyright (c) 2017 - 2018, Andrei Warkentin 
+#
+#  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"

[edk2] [PATCH v2 edk2-platforms 15/20] Platform/Broadcom/RPi3: Add Platform Config driver

2018-12-10 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxe.c| 356 

 Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxe.inf  |  81 +
 Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxeFormSetGuid.h |  23 ++
 Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxeHii.uni   | 100 ++
 Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxeHii.vfr   | 306 
+
 5 files changed, 866 insertions(+)

diff --git a/Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxe.c 
b/Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxe.c
new file mode 100644
index ..4e3c68ca2994
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Drivers/ConfigDxe/ConfigDxe.c
@@ -0,0 +1,356 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *
+ *  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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "ConfigDxeFormSetGuid.h"
+
+extern UINT8 ConfigDxeHiiBin[];
+extern UINT8 ConfigDxeStrings[];
+
+STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL *mFwProtocol;
+
+typedef struct {
+  VENDOR_DEVICE_PATH VendorDevicePath;
+  EFI_DEVICE_PATH_PROTOCOL End;
+} HII_VENDOR_DEVICE_PATH;
+
+STATIC HII_VENDOR_DEVICE_PATH mVendorDevicePath = {
+  {
+{
+  HARDWARE_DEVICE_PATH,
+  HW_VENDOR_DP,
+  {
+(UINT8) (sizeof (VENDOR_DEVICE_PATH)),
+(UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
+  }
+},
+CONFIGDXE_FORM_SET_GUID
+  },
+  {
+END_DEVICE_PATH_TYPE,
+END_ENTIRE_DEVICE_PATH_SUBTYPE,
+{
+  (UINT8) (END_DEVICE_PATH_LENGTH),
+  (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
+}
+  }
+};
+
+
+STATIC EFI_STATUS
+InstallHiiPages (
+  VOID
+  )
+{
+  EFI_STATUS Status;
+  EFI_HII_HANDLE HiiHandle;
+  EFI_HANDLE DriverHandle;
+
+  DriverHandle = NULL;
+  Status = gBS->InstallMultipleProtocolInterfaces (&DriverHandle,
+  &gEfiDevicePathProtocolGuid,
+  &mVendorDevicePath,
+  NULL);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  HiiHandle = HiiAddPackages (&gConfigDxeFormSetGuid,
+  DriverHandle,
+  ConfigDxeStrings,
+  ConfigDxeHiiBin,
+  NULL);
+
+  if (HiiHandle == NULL) {
+gBS->UninstallMultipleProtocolInterfaces (DriverHandle,
+  &gEfiDevicePathProtocolGuid,
+  &mVendorDevicePath,
+  NULL);
+return EFI_OUT_OF_RESOURCES;
+  }
+  return EFI_SUCCESS;
+}
+
+
+STATIC EFI_STATUS
+SetupVariables (
+  VOID
+  )
+{
+  UINTN Size;
+  UINT32 Var32;
+  EFI_STATUS Status;
+
+  /*
+   * Create the vars with default value.
+   * If we don't, forms won't be able to update.
+   */
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable(L"CpuClock",
+&gConfigDxeFormSetGuid,
+NULL,  &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdCpuClock, PcdGet32 (PcdCpuClock));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable(L"SdIsArasan",
+&gConfigDxeFormSetGuid,
+NULL,  &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdSdIsArasan, PcdGet32 (PcdSdIsArasan));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable(L"MmcDisableMulti",
+&gConfigDxeFormSetGuid,
+NULL,  &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcDisableMulti, PcdGet32 (PcdMmcDisableMulti));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable(L"MmcForce1Bit",
+&gConfigDxeFormSetGuid,
+NULL,  &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForce1Bit, PcdGet32 (PcdMmcForce1Bit));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable(L"MmcForceDefaultSpeed",
+&gConfigDxeFormSetGuid,
+NULL,  &Size, &Var32);
+  if (EFI_ERROR (Status)) {
+PcdSet32 (PcdMmcForceDefaultSpeed, PcdGet32 (PcdMmcForceDefaultSpeed));
+  }
+
+  Size = sizeof (UINT32);
+  Status = gRT->GetVariable(L"MmcSdDefaultSpeedMHz"

[edk2] [PATCH v2 edk2-platforms 14/20] Platform/Broadcom/RPi3: Add NV Storage driver

2018-12-10 Thread Pete Batard
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Pete Batard 
---
 Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/FileIo.c   | 
196 
 Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/FvbInfo.c  | 
118 +++
 Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/VarBlockService.c  | 
984 
 Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/VarBlockService.h  | 
217 +
 Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.c   | 
334 +++
 Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/VarBlockServiceDxe.inf |  
93 ++
 6 files changed, 1942 insertions(+)

diff --git a/Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/FileIo.c 
b/Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/FileIo.c
new file mode 100644
index ..961de586c78b
--- /dev/null
+++ b/Platform/Broadcom/Bcm283x/Drivers/VarBlockServiceDxe/FileIo.c
@@ -0,0 +1,196 @@
+/** @file
+ *
+ *  Copyright (c) 2018, Andrei Warkentin 
+ *  Copyright (c) 2007-2009, Intel Corporation. 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 "VarBlockService.h"
+
+
+EFI_STATUS
+FileWrite (
+  IN EFI_FILE_PROTOCOL *File,
+  IN UINTN Offset,
+  IN UINTN Buffer,
+  IN UINTN Size
+  )
+{
+  EFI_STATUS Status;
+
+  Status = File->SetPosition (File, Offset);
+  ASSERT_EFI_ERROR (Status);
+  if (!EFI_ERROR (Status)) {
+Status = File->Write (File, &Size, (VOID *) Buffer);
+ASSERT_EFI_ERROR (Status);
+  }
+  return Status;
+}
+
+
+VOID
+FileClose (
+  IN  EFI_FILE_PROTOCOL *File
+  )
+{
+  File->Flush (File);
+  File->Close (File);
+}
+
+
+EFI_STATUS
+FileOpen (
+  IN  EFI_DEVICE_PATH_PROTOCOL *Device,
+  IN  CHAR16 *MappedFile,
+  OUT EFI_FILE_PROTOCOL **File,
+  IN  UINT64 OpenMode
+  )
+{
+  EFI_HANDLEHandle;
+  EFI_FILE_HANDLE   Root;
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL   *Volume;
+  EFI_STATUSStatus;
+
+  *File = NULL;
+
+  Status = gBS->LocateDevicePath (
+  &gEfiSimpleFileSystemProtocolGuid,
+  &Device,
+  &Handle
+  );
+
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  Status = gBS->HandleProtocol (
+  Handle,
+  &gEfiSimpleFileSystemProtocolGuid,
+  (void **) &Volume
+  );
+  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+return Status;
+  }
+
+  //
+  // Open the root directory of the volume
+  //
+  Root = NULL;
+  Status = Volume->OpenVolume (
+ Volume,
+ &Root
+ );
+  ASSERT_EFI_ERROR (Status);
+  ASSERT (Root != NULL);
+
+  //
+  // Open file
+  //
+  Status = Root->Open (
+   Root,
+   File,
+   MappedFile,
+   OpenMode,
+   0
+   );
+  if (EFI_ERROR (Status)) {
+*File = NULL;
+  }
+
+  //
+  // Close the Root directory
+  //
+  Root->Close (Root);
+  return Status;
+}
+
+
+EFI_STATUS
+CheckStore (
+  IN  EFI_HANDLE SimpleFileSystemHandle,
+  OUT EFI_DEVICE_PATH_PROTOCOL **Device
+  )
+{
+  EFI_STATUS Status;
+  EFI_BLOCK_IO_PROTOCOL *BlkIo;
+  EFI_FILE_PROTOCOL *File;
+
+  *Device = NULL;
+  Status  = gBS->HandleProtocol (
+   SimpleFileSystemHandle,
+   &gEfiBlockIoProtocolGuid,
+   (VOID*)&BlkIo
+   );
+
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+  if (!BlkIo->Media->MediaPresent) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media not present!\n"));
+Status = EFI_NO_MEDIA;
+goto ErrHandle;
+  }
+  if (BlkIo->Media->ReadOnly) {
+DEBUG ((DEBUG_ERROR, "FwhMappedFile: Media is read-only!\n"));
+Status = EFI_ACCESS_DENIED;
+goto ErrHandle;
+  }
+
+  Status = FileOpen (DevicePathFromHandle (SimpleFileSystemHandle),
+ mFvInstance->MappedFile, &File,
+ EFI_FILE_MODE_READ);
+  if (EFI_ERROR (Status)) {
+goto ErrHandle;
+  }
+
+  /* We found it! Maybe do more checks...? */
+
+  FileClose (File);
+  *Device = DuplicateDevicePath (DevicePathFromHandle 
(SimpleFileSystemHandle));
+
+  ASSERT (*Device != NULL);
+
+ErrHandle:
+  return Status;
+}
+
+
+EFI_STATUS
+CheckStoreExists (
+  IN  EFI_DEVICE_PATH_PROTOCOL *Device
+  )
+

  1   2   3   >