3 * Copyright (c) 2011-2015, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "LinuxAtag.h"
16 #include "LinuxLoader.h"
18 // Point to the current ATAG
19 STATIC LINUX_ATAG
*mLinuxKernelCurrentAtag
;
27 mLinuxKernelCurrentAtag
->header
.size
= tag_size (LINUX_ATAG_CORE
);
28 mLinuxKernelCurrentAtag
->header
.type
= ATAG_CORE
;
30 mLinuxKernelCurrentAtag
->body
.core_tag
.flags
= 1; /* ensure read-only */
31 mLinuxKernelCurrentAtag
->body
.core_tag
.pagesize
= PageSize
; /* systems PageSize (4k) */
32 mLinuxKernelCurrentAtag
->body
.core_tag
.rootdev
= 0; /* zero root device (typically overridden from kernel command line )*/
34 // move pointer to next tag
35 mLinuxKernelCurrentAtag
= next_tag_address (mLinuxKernelCurrentAtag
);
41 IN UINTN StartAddress
,
45 mLinuxKernelCurrentAtag
->header
.size
= tag_size (LINUX_ATAG_MEM
);
46 mLinuxKernelCurrentAtag
->header
.type
= ATAG_MEM
;
48 mLinuxKernelCurrentAtag
->body
.mem_tag
.start
= StartAddress
; /* Start of memory chunk for AtagMem */
49 mLinuxKernelCurrentAtag
->body
.mem_tag
.size
= Size
; /* Size of memory chunk for AtagMem */
51 // move pointer to next tag
52 mLinuxKernelCurrentAtag
= next_tag_address (mLinuxKernelCurrentAtag
);
58 IN CONST CHAR8
*CmdLine
63 // Increment the line length by 1 to account for the null string terminator character
64 LineLength
= AsciiStrLen (CmdLine
) + 1;
66 /* Check for NULL strings.
67 * Do not insert a tag for an empty CommandLine, don't even modify the tag address pointer.
68 * Remember, you have at least one null string terminator character.
71 mLinuxKernelCurrentAtag
->header
.size
= ((UINT32
)sizeof (LINUX_ATAG_HEADER
) + LineLength
+ (UINT32
)3) >> 2;
72 mLinuxKernelCurrentAtag
->header
.type
= ATAG_CMDLINE
;
74 /* place CommandLine into tag */
75 AsciiStrCpyS (mLinuxKernelCurrentAtag
->body
.cmdline_tag
.cmdline
, LineLength
, CmdLine
);
77 // move pointer to next tag
78 mLinuxKernelCurrentAtag
= next_tag_address (mLinuxKernelCurrentAtag
);
85 IN UINT32 InitrdImage
,
86 IN UINT32 InitrdImageSize
89 mLinuxKernelCurrentAtag
->header
.size
= tag_size (LINUX_ATAG_INITRD2
);
90 mLinuxKernelCurrentAtag
->header
.type
= ATAG_INITRD2
;
92 mLinuxKernelCurrentAtag
->body
.initrd2_tag
.start
= InitrdImage
;
93 mLinuxKernelCurrentAtag
->body
.initrd2_tag
.size
= InitrdImageSize
;
95 // Move pointer to next tag
96 mLinuxKernelCurrentAtag
= next_tag_address (mLinuxKernelCurrentAtag
);
104 // Empty tag ends list; this has zero length and no body
105 mLinuxKernelCurrentAtag
->header
.type
= ATAG_NONE
;
106 mLinuxKernelCurrentAtag
->header
.size
= 0;
108 /* We can not calculate the next address by using the standard macro:
109 * Params = next_tag_address (Params);
110 * because it relies on the header.size, which here it is 0 (zero).
111 * The easiest way is to add the sizeof (mLinuxKernelCurrentAtag->header).
113 mLinuxKernelCurrentAtag
= (LINUX_ATAG
*)((UINT32
)mLinuxKernelCurrentAtag
+ sizeof (mLinuxKernelCurrentAtag
->header
));
118 IN EFI_PHYSICAL_ADDRESS SystemMemoryBase
,
119 IN CONST CHAR8
* CommandLineString
,
120 IN EFI_PHYSICAL_ADDRESS InitrdImage
,
121 IN UINTN InitrdImageSize
,
122 OUT EFI_PHYSICAL_ADDRESS
*AtagBase
,
127 LIST_ENTRY
*ResourceLink
;
128 LIST_ENTRY ResourceList
;
129 EFI_PHYSICAL_ADDRESS AtagStartAddress
;
130 SYSTEM_MEMORY_RESOURCE
*Resource
;
132 AtagStartAddress
= LINUX_ATAG_MAX_OFFSET
;
133 Status
= gBS
->AllocatePages (AllocateMaxAddress
, EfiBootServicesData
, EFI_SIZE_TO_PAGES (ATAG_MAX_SIZE
), &AtagStartAddress
);
134 if (EFI_ERROR (Status
)) {
135 DEBUG ((EFI_D_WARN
, "Warning: Failed to allocate Atag at 0x%lX (%r). The Atag will be allocated somewhere else in System Memory.\n", AtagStartAddress
, Status
));
136 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesData
, EFI_SIZE_TO_PAGES (ATAG_MAX_SIZE
), &AtagStartAddress
);
137 if (EFI_ERROR (Status
)) {
142 // Ready to setup the atag list
143 mLinuxKernelCurrentAtag
= (LINUX_ATAG
*)(UINTN
)AtagStartAddress
;
145 // Standard core tag 4k PageSize
146 SetupCoreTag ( (UINT32
)SIZE_4KB
);
148 // Physical memory setup
149 GetSystemMemoryResources (&ResourceList
);
150 ResourceLink
= ResourceList
.ForwardLink
;
151 while (ResourceLink
!= NULL
&& ResourceLink
!= &ResourceList
) {
152 Resource
= (SYSTEM_MEMORY_RESOURCE
*)ResourceLink
;
153 DEBUG ((EFI_D_INFO
, "- [0x%08X,0x%08X]\n",
154 (UINT32
)Resource
->PhysicalStart
,
155 (UINT32
)Resource
->PhysicalStart
+ (UINT32
)Resource
->ResourceLength
));
156 SetupMemTag ((UINT32
)Resource
->PhysicalStart
, (UINT32
)Resource
->ResourceLength
);
157 ResourceLink
= ResourceLink
->ForwardLink
;
160 // CommandLine setting root device
161 if (CommandLineString
) {
162 SetupCmdlineTag (CommandLineString
);
165 if (InitrdImageSize
> 0 && InitrdImage
!= 0) {
166 SetupInitrdTag ((UINT32
)InitrdImage
, (UINT32
)InitrdImageSize
);
172 // Calculate atag list size
173 *AtagBase
= AtagStartAddress
;
174 *AtagSize
= (UINT32
)mLinuxKernelCurrentAtag
- (UINT32
)AtagStartAddress
+ 1;