cancel
Showing results for 
Search instead for 
Did you mean: 

BKPSRAM access in STM32MP1x MPU

asydhom
Associate II

We need to store some of our application data in BKPSRAM block so we can persist it during power down. Here is the code that we created to write to the BKPSRAM block, could you please help me to define what is wrong. Thanks.

#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdint.h>
#include <sys/stat.h>
#include <string.h>

#define PAGE_SIZE 4096

typedef struct __attribute__((packed)) {
    uint32_t value1;
    uint32_t value2;
    char str[24];
} MyStruct;


int main() {
    int fd;
    uint32_t *mapped_memory;

    // Open /dev/mem
    fd = open("/dev/mem", O_RDWR | O_SYNC);
    if (fd == -1) {
        perror("open");
        return 1;
    }

    // Calculate the page-aligned address
    off_t base_addr = 0x54000000U;
    off_t aligned_addr = base_addr & ~(PAGE_SIZE - 1);

    // Calculate the offset from the page boundary
    off_t offset = base_addr - aligned_addr;

    // Map the aligned address with the required size
    mapped_memory = mmap(NULLoffset + sizeof(MyStruct), PROT_READ | PROT_WRITE, MAP_SHARED, fdaligned_addr);
    if (mapped_memory == MAP_FAILED) {
        perror("mmap");
        close(fd);
        return 1;
    }

    // Calculate the pointer to the desired address
    MyStruct *myStruct = (MyStruct *)((uintptr_t)mapped_memory + offset);

    // Write data to BKPSRAM
    myStruct->value1 = 0xDEADBEEF;
    printf("Data read from BKPSRAM: 0x%08x\n",  myStruct->value1);

    myStruct->value2 = 0xFFFFEEEE;
    printf("Data read from BKPSRAM: 0x%08x\n",  myStruct->value2);
    char str[] = "Hello World!\0";
    for(int i = 0i < sizeof(str)/sizeof(char); i++){
        myStruct->str[i= str[i];
        printf("%c"myStruct->str[i]);
    }

    printf("\n");
    // Unmap the memory
    if (munmap(mapped_memory, offset + sizeof(MyStruct)) == -1) {
        perror("munmap");
        close(fd);
        return 1;
    }
    // Close /dev/mem
    close(fd);
    return 0;
}
2 REPLIES 2
Olivier GALLIEN
ST Employee

Hi @asydhom ,

 

Not diiging into your code yet. 

Would be very good to get some description of the behavior first .. any logs to share ?

 

Else I also point to this page BKPSRAM internal memory - stm32mpu

 

By default BKPSRAM is SECURED and so not accessible to write operation. It require a change in TF-A to set it back to non-secure. 

 

Hope it help 

 

Olivier 

Olivier GALLIEN
In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Hi Olivier,

 

I forwarded your reply to our customer and here its response with the changes he did to make the BKPSRAM block non-secure.

======================================================================================

There aren't any logs, all I am trying to do is write to the BKPSRAM. When I use mmap in a write.c and then mmap read.c, I cannot read back what I have written. 

I have already done DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_NS_RW, DECPROT_UNLOCK) in optee.dts

and I have already done 


reserved-memory {
#address-cells = <1>;
#size-cells = <1>;
ranges;

/* USER CODE BEGIN reserved-memory */
optee_framebuffer@dd000000 {
reg = <0xdd000000 0x1000000>;
no-map;
};
optee@de000000{
reg = <0xde000000 0x2000000>;
no-map;
};

bkpsram@54000000 {
reg = <0x54000000 0xBB8>;
no-map;

};
/* USER CODE END reserved-memory */
};

in kernel.dts