Search code examples
linux-kernelenvironment-variablesembedded-linuxkernel-moduleu-boot

Adding Custom environment variable to UBoot and access it from Kernal module


I want to add a custom variable to the U-Boot environment, and then access it from a Linux kernel module. Is this possible?
If is is possible please help me to solve this problem.
For example we can print

printenv

It will display environment variables. I want to add extra variable to that list. Is this approach is good?
Or is there any other way?


Solution

  • Is this approach is good ?

    IMO no, it is not good.
    The U-Boot runtime environment would be in main memory, at a location unknown to the Linux kernel. When the kernel gains control of the CPU, this memory buffer containing U-Boot's environment variables is probably part of Linux "free" memory. To preserve this buffer for access by the kernel, you would have to hack U-Boot to relocate this environment buffer to a safe location, and somehow pass this location to the kernel.
    IOW you would have to create a new boot-program-to-kernel interface.

    Alternatively your new custom environment variable(s) could be saved to persistent storage. But that creates other issues.
    Depending on the boot device, accessing U-Boot's environment storage could be raw NAND or raw sectors of a SDcard or a file in a VFAT filesystem on a SDcard. Typically the Linux kernel does not even know (much less care) which boot medium was used or where that boot program resides.
    But your idea would require that information. Additionally the kernel would have to read that environment, which is considered bad practice. (What is easy to do during boot becomes complicated in a secure kernel.) See Driving Me Nuts - Things You Never Should Do in the Kernel.

    or is there is any other way ?

    You would be better off converting the variable name and its value to a text string, and appending it to the bootargs variable.
    Prefix the parameter with your module's name to avoid conflicts.

    my_module.my_variable=value
    

    Then your kernel module can retrieve it as described in Passing Command Line Arguments to a Module
    This approach avoids polluting the kernel with new dependencies on boot-program data storage.
    It uses an existing kernel interface, the kernel command line.