3 * Copyright (c) 2011-2012, 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 "BdsInternal.h"
16 #include "BdsLinuxLoader.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 AsciiStrCpy(mLinuxKernelCurrentAtag
->body
.cmdline_tag
.cmdline
, 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 CONST CHAR8
* CommandLineString
,
119 IN EFI_PHYSICAL_ADDRESS InitrdImage
,
120 IN UINTN InitrdImageSize
,
121 OUT EFI_PHYSICAL_ADDRESS
*AtagBase
,
126 LIST_ENTRY
*ResourceLink
;
127 LIST_ENTRY ResourceList
;
128 EFI_PHYSICAL_ADDRESS AtagStartAddress
;
129 BDS_SYSTEM_MEMORY_RESOURCE
*Resource
;
131 AtagStartAddress
= LINUX_ATAG_MAX_OFFSET
;
132 Status
= gBS
->AllocatePages (AllocateMaxAddress
, EfiBootServicesData
, EFI_SIZE_TO_PAGES(ATAG_MAX_SIZE
), &AtagStartAddress
);
133 if (EFI_ERROR(Status
)) {
134 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
));
135 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesData
, EFI_SIZE_TO_PAGES(ATAG_MAX_SIZE
), &AtagStartAddress
);
136 ASSERT_EFI_ERROR(Status
);
139 // Ready to setup the atag list
140 mLinuxKernelCurrentAtag
= (LINUX_ATAG
*)(UINTN
)AtagStartAddress
;
142 // Standard core tag 4k PageSize
143 SetupCoreTag( (UINT32
)SIZE_4KB
);
145 // Physical memory setup
146 GetSystemMemoryResources (&ResourceList
);
147 ResourceLink
= ResourceList
.ForwardLink
;
148 while (ResourceLink
!= NULL
&& ResourceLink
!= &ResourceList
) {
149 Resource
= (BDS_SYSTEM_MEMORY_RESOURCE
*)ResourceLink
;
150 DEBUG((EFI_D_INFO
,"- [0x%08X,0x%08X]\n",(UINT32
)Resource
->PhysicalStart
,(UINT32
)Resource
->PhysicalStart
+(UINT32
)Resource
->ResourceLength
));
151 SetupMemTag( (UINT32
)Resource
->PhysicalStart
, (UINT32
)Resource
->ResourceLength
);
152 ResourceLink
= ResourceLink
->ForwardLink
;
155 // CommandLine setting root device
156 if (CommandLineString
) {
157 SetupCmdlineTag (CommandLineString
);
160 if (InitrdImageSize
> 0 && InitrdImage
!= 0) {
161 SetupInitrdTag ((UINT32
)InitrdImage
, (UINT32
)InitrdImageSize
);
167 // Calculate atag list size
168 *AtagBase
= AtagStartAddress
;
169 *AtagSize
= (UINT32
)mLinuxKernelCurrentAtag
- (UINT32
)AtagStartAddress
+ 1;