@web-font-path: "roboto-debian.css";
Loading...
Searching...
No Matches
boot_lock.h
1/*
2 * Copyright (c) 2024 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _HARDWARE_BOOT_LOCK_H
8#define _HARDWARE_BOOT_LOCK_H
9
10#include "pico.h"
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_BOOT_LOCK, Enable/disable assertions in the hardware_boot_lock module, type=bool, default=0, group=hardware_boot_lock
17#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_BOOT_LOCK
18#define PARAM_ASSERTIONS_ENABLED_HARDWARE_BOOT_LOCK 0
19#endif
20
21#if NUM_BOOT_LOCKS > 0
22#include "hardware/sync.h"
23#include "hardware/structs/bootram.h"
24
28typedef volatile uint32_t boot_lock_t;
29
36__force_inline static boot_lock_t *boot_lock_instance(uint lock_num) {
37 invalid_params_if(HARDWARE_BOOT_LOCK, lock_num >= NUM_BOOT_LOCKS);
38 return (boot_lock_t *) (BOOTRAM_BASE + BOOTRAM_BOOTLOCK0_OFFSET + lock_num * 4);
39}
40
47__force_inline static uint boot_lock_get_num(boot_lock_t *lock) {
48 invalid_params_if(HARDWARE_BOOT_LOCK, (uint) lock < BOOTRAM_BASE + BOOTRAM_BOOTLOCK0_OFFSET ||
49 (uint) lock >= NUM_BOOT_LOCKS * sizeof(boot_lock_t) + BOOTRAM_BASE + BOOTRAM_BOOTLOCK0_OFFSET ||
50 ((uint) lock - BOOTRAM_BASE + BOOTRAM_BOOTLOCK0_OFFSET) % sizeof(boot_lock_t) != 0);
51 return (uint) (lock - (boot_lock_t *) (BOOTRAM_BASE + BOOTRAM_BOOTLOCK0_OFFSET));
52}
53
59__force_inline static void boot_lock_unsafe_blocking(boot_lock_t *lock) {
60 // Note we don't do a wfe or anything, because by convention these boot_locks are VERY SHORT LIVED and NEVER BLOCK and run
61 // with INTERRUPTS disabled (to ensure that)... therefore nothing on our core could be blocking us, so we just need to wait on another core
62 // anyway which should be finished soon
63 while (__builtin_expect(!*lock, 0)) { // read from bootlock register (tries to acquire the lock)
65 }
67}
68
74__force_inline static bool boot_try_lock_unsafe(boot_lock_t *lock) {
75 if (*lock) {
77 return true;
78 }
79 return false;
80}
81
87__force_inline static void boot_unlock_unsafe(boot_lock_t *lock) {
89 *lock = 0; // write to bootlock register (release lock)
90}
91
100__force_inline static uint32_t boot_lock_blocking(boot_lock_t *lock) {
101 uint32_t save = save_and_disable_interrupts();
102 boot_lock_unsafe_blocking(lock);
103 return save;
104}
105
111inline static bool is_boot_locked(boot_lock_t *lock) {
112 check_hw_size(boot_lock_t, 4);
113 uint lock_num = boot_lock_get_num(lock);
114 return 0 != (*(io_ro_32 *) (BOOTRAM_BASE + BOOTRAM_BOOTLOCK_STAT_OFFSET) & (1u << lock_num));
115}
116
127__force_inline static void boot_unlock(boot_lock_t *lock, uint32_t saved_irq) {
128 boot_unlock_unsafe(lock);
130}
131
140boot_lock_t *boot_lock_init(uint lock_num);
141
145void boot_locks_reset(void);
146
147#endif
148
149#ifdef __cplusplus
150}
151#endif
152
153#endif
static __force_inline uint32_t save_and_disable_interrupts(void)
Disable interrupts on the calling core, returning the previous interrupt state.
Definition sync.h:237
static __force_inline void __mem_fence_release(void)
Release a memory fence.
Definition sync.h:189
static __force_inline void restore_interrupts_from_disabled(uint32_t status)
Restore interrupts to a specified state on the calling core with restricted transitions.
Definition sync.h:280
static __force_inline void __mem_fence_acquire(void)
Acquire a memory fence.
Definition sync.h:173
#define __force_inline
Attribute to force inlining of a function regardless of optimization level.
Definition compiler.h:125
static __force_inline void tight_loop_contents(void)
No-op function for the body of tight loops.
Definition platform.h:91