FlashMyPico.com
log in about discover docs scratchpad
This website requires a Chrome-based browser in order to access the WebUSB API. Without it, you may not be able to flash your device.

runtime_flash_permissions
for Raspberry Pi Picoβ„’, submitted by flashmypico

Loading...

/** * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause */ #include <stdio.h> #include "pico/stdlib.h" #include "pico/bootrom.h" #include "boot/picobin.h" int main() { stdio_init_all(); static uint8_t flash_buffer[0x1000] = {}; cflash_flags_t flags; flags.flags = (CFLASH_OP_VALUE_READ << CFLASH_OP_LSB) | (CFLASH_SECLEVEL_VALUE_SECURE << CFLASH_SECLEVEL_LSB); int ret; // Unpartitioned space only has bootloader permissions, so secure reads will fail printf("Attempt to read start of flash\n"); ret = rom_flash_op(flags, XIP_BASE, sizeof(flash_buffer), flash_buffer); if (ret == BOOTROM_ERROR_NOT_PERMITTED) { printf("Not permitted, as expected\n"); } else { printf("ERROR: "); if (ret) { printf("Flash OP failed with %d\n", ret); } else { printf("Flash OP succeeded, when shouldn't have been permitted"); } } // Add a runtime partition at start of flash with all permissions, to allow access to that region of flash printf("Adding partition with all permissions\n"); ret = rom_add_flash_runtime_partition(0, 0x1000, PICOBIN_PARTITION_PERMISSIONS_BITS); if (ret != 0) { printf("ERROR: Failed to add runtime partition\n"); } // Reads from that partition will now succeed printf("Attempt to read start of flash\n"); ret = rom_flash_op(flags, XIP_BASE, sizeof(flash_buffer), flash_buffer); if (ret == 0) { printf("Read successful - read first 2 words %08x %08x\n", ((uint32_t*)flash_buffer)[0], ((uint32_t*)flash_buffer)[1]); } else { printf("ERROR: Flash OP failed with %d\n", ret); } // But reads from unpartitioned space will still fail printf("Attempt to read later in flash\n"); ret = rom_flash_op(flags, XIP_BASE + 0x1000, sizeof(flash_buffer), flash_buffer); if (ret == BOOTROM_ERROR_NOT_PERMITTED) { printf("Not permitted, as expected\n"); } else { printf("ERROR: "); if (ret) { printf("Flash OP failed with %d\n", ret); } else { printf("Flash OP succeeded, when it shouldn't have been permitted - read first 2 words %08x %08x\n", ((uint32_t*)flash_buffer)[0], ((uint32_t*)flash_buffer)[1]); } } }
This firmware image was imported from the pico-examples repository. 


Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
   disclaimer.

2. 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.

3. Neither the name of the copyright holder 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.
Homepage: https://github.com/raspberrypi/pico-examples
Repository: https://github.com/raspberrypi/pico-examples/tree/master/flash/runtime_flash_permissions

β“˜ Tips