From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nhPJU-0005SE-Bq for mharc-grub-devel@gnu.org; Thu, 21 Apr 2022 01:25:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42750) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nhPJS-0005Nr-J7 for grub-devel@gnu.org; Thu, 21 Apr 2022 01:25:06 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]:42592) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nhPJQ-0005fh-SK for grub-devel@gnu.org; Thu, 21 Apr 2022 01:25:06 -0400 Received: by mail-pf1-x430.google.com with SMTP id j17so3961226pfi.9 for ; Wed, 20 Apr 2022 22:25:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XDAbfKuASRW37uGKBWYknqK+ZeChCSfJGqNQHGqyVSA=; b=nNmm41NIR6W4F07mBzlHy2Wc1HzaVySqr5JoSkG2QF0Vnmao1zp2j/4PQLQsN6nu+c xvezhQO5aC4FuGdaPNcfT50viIXND67WR83vUdb3WUPfPeINNvUWz2jsc388jpWSDVME Vonp+7rfF6SubDhFwIk4OMa9dZ294wHlsvICA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XDAbfKuASRW37uGKBWYknqK+ZeChCSfJGqNQHGqyVSA=; b=36t9nD/JMx2OC5cqv89XqrYbqDot3PMXPgycS/6SuwMj/HsDu97ktGoPrcbK+P/ZQL D7pULo0AdGj+QFZnl76CJmyaFsd1tB8YDYU1CK/1RQtGkIDeWpGFsgy8Pfmr/uA7fPkk h9wiwqHpdx9pB5fzX+b/f4OYQXm9kU+cIGbRFjQnwQeLjcEEjg7HSQwHc6/AXLjr3qCX AvHSpGlP3wha7F7M9M9Fcdz4bg505f70vjUJhsBVLZ2wwQrSrLrbz1TyCOqL7T96B/g6 RjmXMi7WV2ZEk/gjZ1CGzjwgGxZhHXAq0qqnRnpbRRr2Bqx90ZcY1VwZlUqg8Pmg1B62 1r6A== X-Gm-Message-State: AOAM533t7iwmbD08etqz1TC32g5BJe/ag3AlAh3aZ1k9Gomqe8zVLS2v tMz13DE53xo24fX5/rKQ8VQIKXeUvNgQ3w== X-Google-Smtp-Source: ABdhPJwZgkQZVolN/S50oijbesYOwAbbZzytW3QY5jtiAuALrtS7E3qcvdnjF5EpMxJODgseXTtEpA== X-Received: by 2002:a65:6d0c:0:b0:382:a4b1:c93 with SMTP id bf12-20020a656d0c000000b00382a4b10c93mr23085726pgb.237.1650518703358; Wed, 20 Apr 2022 22:25:03 -0700 (PDT) Received: from localhost ([2001:4479:e000:e400:4260:be14:52a8:8c94]) by smtp.gmail.com with ESMTPSA id g15-20020aa7818f000000b00505ce2e4640sm21358570pfi.100.2022.04.20.22.25.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 22:25:03 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: leif@nuviainc.com, stefanb@linux.ibm.com, ps@pks.im, dkiper@net-space.pl, Daniel Axtens , Daniel Kiper Subject: [PATCH v3 08/15] efi: mm: Extract function to add memory regions Date: Thu, 21 Apr 2022 15:24:20 +1000 Message-Id: <20220421052427.1389987-9-dja@axtens.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220421052427.1389987-1-dja@axtens.net> References: <20220421052427.1389987-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=dja@axtens.net; helo=mail-pf1-x430.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Apr 2022 05:25:06 -0000 From: Patrick Steinhardt In preparation of support for runtime-allocating additional memory region, this patch extracts the function to retrieve the EFI memory map and add a subset of it to GRUB's own memory regions. Signed-off-by: Patrick Steinhardt Signed-off-by: Daniel Axtens Reviewed-by: Daniel Kiper --- grub-core/kern/efi/mm.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index 0bccd24f304f..c8d3968f7f68 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -556,8 +556,8 @@ print_memory_map (grub_efi_memory_descriptor_t *memory_map, } #endif -void -grub_efi_mm_init (void) +static grub_err_t +grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes) { grub_efi_memory_descriptor_t *memory_map; grub_efi_memory_descriptor_t *memory_map_end; @@ -570,7 +570,7 @@ grub_efi_mm_init (void) /* Prepare a memory region to store two memory maps. */ memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); if (! memory_map) - grub_fatal ("cannot allocate memory"); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for memory map"); /* Obtain descriptors for available memory. */ map_size = MEMORY_MAP_SIZE; @@ -588,14 +588,14 @@ grub_efi_mm_init (void) memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (map_size)); if (! memory_map) - grub_fatal ("cannot allocate memory"); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for new memory map"); mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0); } if (mm_status < 0) - grub_fatal ("cannot get memory map"); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "error fetching memory map from EFI"); memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size); @@ -610,7 +610,7 @@ grub_efi_mm_init (void) /* Allocate memory regions for GRUB's memory management. */ add_memory_regions (filtered_memory_map, desc_size, - filtered_memory_map_end, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE)); + filtered_memory_map_end, BYTES_TO_PAGES (required_bytes)); #if 0 /* For debug. */ @@ -628,6 +628,15 @@ grub_efi_mm_init (void) /* Release the memory maps. */ grub_efi_free_pages ((grub_addr_t) memory_map, 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); + + return GRUB_ERR_NONE; +} + +void +grub_efi_mm_init (void) +{ + if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE) != GRUB_ERR_NONE) + grub_fatal ("%s", grub_errmsg); } #if defined (__aarch64__) || defined (__arm__) || defined (__riscv) -- 2.32.0