jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

[SOLVED] Select an alternate config.txt file?

Wed Apr 21, 2021 7:24 pm

I have an application where I want to install several different operating systems on the same device, (in my case a USB SSD by Seagate), and the way the system is organized is like this:

Partition 1, (FAT-32 boot partition) contains:
Directories:
* OS1
* OS2
* OS3
Each of these directories has the entire and complete set of boot files from the boot partition of the associated image file.

Files:
All the fixup[xx] and start[xx].elf files from a recent Raspbian/Raspberry Pi O/S based on Debian Buster. (I"m using this on a Pi-4)
A copy of the config.txt file from one of the three OS folders, which contains an os_prefix= line that points to OS1, OS2 or OS3.

Apparently, the os_prefix=, when it points to a particular boot sub-directory, does not re-read the config.txt in that directory, though it appears to read the cmdline.txt file.

If I create a skeleton config.txt file and include GPIO pin conditionals [gpio26=0] to specify an os_prefix, there are issues with the operating systems as booted - certain things apparently don't get read. If I copy the config.txt file from the operating system's boot subdirectory, (which includes an os_prefix= statement at the end), everything works wonderfully.

Partitions 5, 6, 7, contain the files and folders from the rootfs partition of the associated image.
Partition 8 is a global data partition for all of the operating systems to use.

What I would like to be able to do, (if possible) is to copy the actual config.txt files to the boot partition, name them something like OS1config.txt, OS2config.txt and OS3config.txt and then have a global config.txt file that does something like

Code: Select all

[set up gpio pins as inputs]
[maybe more stuff]

[gpio21=0]
(jump to OS1config.txt and continue from there)

[gpio26=0]
(jump to OS2config.txt as above)

[gpio16=0]
(jump to OS3config.txt. . . .)

and so on. . .
Is this possible?
Last edited by jharris1993 on Thu Apr 22, 2021 5:27 pm, edited 1 time in total.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

MiscBits
Posts: 203
Joined: Wed Jan 27, 2021 12:48 pm

Re: Select an alternate config.txt file?

Wed Apr 21, 2021 8:25 pm

Sorry - not really an answer to the question but an idea...

I would look at PINN rather than try it myself :-)

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Re: Select an alternate config.txt file?

Thu Apr 22, 2021 7:21 am

I've already done that, and contributed to the literature on the subject.

One thing I've noticed is that PINN, especially after a backup and restore, seems to subtly change things in ways that are not yet fully understood, sometimes creating subtle failures.

I do not know that this is absolutely true, nor do I know the exact mechanism of the changes, if any.

Because of this, I want to explore different methods of multi-booting the Pi so I can compare and contrast them.

One method is to rsync the boot and root partitions to the target media and then run certain necessary fixups to set PARTUUID and boot paths for each OS.

By doing this, I hope to learn more about the multi-boot process and possibly contribute fixes to PINN.

Thanks!
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Select an alternate config.txt file?

Thu Apr 22, 2021 11:21 am

Shameless self reference: viewtopic.php?f=29&t=308692#p1846516
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Re: Select an alternate config.txt file?

Thu Apr 22, 2021 3:10 pm

I am already doing something like this by following the instructions given in https://www.spket.com/blog/raspberry-pi ... dual-boot/

Using this method, the various images are rsync'd to sub-directories in /boot and in individual partitions for the root files.

There are a few necessary fixups to mount a subdirectory on /boot and to make the PARTUUID's line up, but other than that it's a relatively simple process. (famous last words!)

Part of the problem is that I do not know how much of which config file is executed when - is it the config file in /boot or in /boot/[os_name]. Actually, it appears to be at least some of both.

The specification given by this method is to copy the entire config.txt from /boot/[os_name] to /boot - and that appears to work properly. However, I want to be able to select a boot configuration based on particular GPIO pins being pulled low - so, (IMHO) the best way would be to have a particular pin force a jump to a particular config.txt file that is specific to the O/S version I want to boot.

I haven't found anything about that in the documentation and I don't know if this is even possible.

Hopefully someone here will know more about this than I do.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Select an alternate config.txt file?

Thu Apr 22, 2021 5:00 pm

jharris1993 wrote:
Thu Apr 22, 2021 3:10 pm
The specification given by this method is to copy the entire config.txt from /boot/[os_name] to /boot - and that appears to work properly. However, I want to be able to select a boot configuration based on particular GPIO pins being pulled low - so, (IMHO) the best way would be to have a particular pin force a jump to a particular config.txt file that is specific to the O/S version I want to boot.

I haven't found anything about that in the documentation and I don't know if this is even possible.

Hopefully someone here will know more about this than I do.
config.txt has an include directve (see https://www.raspberrypi.org/documentati ... xt/misc.md)

I've not tired it but a minimal config.txt coupled with an os_prefix, and a suitable include directive to a differently named config file may do the trick.

Not sure which of these would be needed though:

Code: Select all

[gpio fileter goes here]
os_prefix=a
[all]
include extraconfig.txt
or

Code: Select all

[gpio fileter goes here]
os_prefix=a
include /a/extraconfig.txt
[all]
Then there's this:
Include directives are not supported by bootcode.bin or the EEPROM bootloader
so it may not work at all on a 4B/CM4. In which case you'd need to put all the OS specific configuration into your main config.txt inside each filter.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Solution to Select an alternate config.txt file?

Thu Apr 22, 2021 5:26 pm

Update:

It turns out that this IS indeed possible, but you have to look under "Misc" in the config.txt documentation to find it.

Viz.:
Config.txt includes an "include" statement of the form include [name of file] where "file" is a config.txt formatted file. Note the absence of an equals sign in the include statement.

You can conditionally include them, such as:

Code: Select all

[gpio16=0]
include first_config.txt

[gpio17=0]
include second_config.txt
Or by using any other conditional expression that's legal in a config.txt file.

Note the following things I discovered while experimenting with this.
1. The include statement is not an unconditional jump to the named file. It simply includes the text of the file inline, (or appended, I'm not sure), to the existing config.txt file.

2. All conditional expressions are evaluated. What this means is that if you have multiple conditional expressions, and each one of them includes a configuration file, every expression that evaluates true will include it's file.

What this means is that you can create customized config files "on-the-fly" by evaluating various conditions, (Pi-3? Pi-4? etc.) and only including the data needed for a specific configuration. It also means that if you have several mutually exclusive conditions set, the last one wins.

3. GPIO setup statements are inherited by subsequent files as if they were a part of the original file. For example, you can have a GPIO pin that selects either the 32 bit kernel or the 64 bit kernel and you can configure it at the very beginning of your base configuration file. Subsequent included file inherit the configuration and can test declared and configured pins without repeating the configuration.

Here's an example of how you can use it. Note that this is a skeleton and much configuration is not included for the sake of brevity.

"base" config.txt file, named "config.txt" which is the main config file.

Code: Select all

# base config.txt file that starts everything going

# setup some GPIO pins for selection purposes as inputs, pulled-high. (short to ground to set to zero)
gpio=16=ip,pu
gpio=17=ip,pu
gpio=18=ip,pu
gpio=19=ip,pu

[gpio16=0]
include Rasp32_config.txt

[gpio17=0]
include Rasp64_config.txt

[gpio18=0]
include Arch_config.txt

# Note the absence of a gpio19 statement, that comes later.
File Rasp32_config.txt is placed in the same directory as the original config.txt file.

Code: Select all

# Rasp32_config.txt file that configures and boots Raspbian 32 bit.

. . . . . .  all the kinds of statements you would expect in a Raspbian config.txt file for this version . . . .

#  Did we want to select the 64 bit kernel instead of the default 32 bit one?
#  We pull gpio 19 to ground to select it.
[gpio19=0]
arm_64bit=1
Note that the normal config.txt files for each of the other operating systems can be included in the same way.

Also note that, in this example, I am selecting individual operating systems to multi-boot, using the configuration noted in https://www.spket.com/blog/raspberry-pi ... dual-boot/

This is not limited to multi-booting, though it is convenient for it - it can be used in any way you wish so long as you create valid config.txt compatible expressions to evaluate. The form and syntax of these statements are outside the scope of this article, but I am sure a bit of research will be fruitful.

There is additional information on the config.txt file in general, and conditional filters in particular, located here:
https://www.raspberrypi.org/documentati ... itional.md
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Re: Select an alternate config.txt file?

Thu Apr 22, 2021 5:49 pm

thagrol wrote:
Thu Apr 22, 2021 5:00 pm
config.txt has an include directve (see https://www.raspberrypi.org/documentati ... xt/misc.md)

I've not tired it but a minimal config.txt coupled with an os_prefix, and a suitable include directive to a differently named config file may do the trick.

Not sure which of these would be needed though:

Code: Select all

[gpio fileter goes here]
os_prefix=a
[all]
include extraconfig.txt
or

Code: Select all

[gpio fileter goes here]
os_prefix=a
include /a/extraconfig.txt
[all]
Then there's this:
Include directives are not supported by bootcode.bin or the EEPROM bootloader
so it may not work at all on a 4B/CM4. In which case you'd need to put all the OS specific configuration into your main config.txt inside each filter.
I must have replied while YOU were replying too - I discovered that and noted my results above.

It may be interesting to note that I am using this on a Pi-4B - 4gb, booting directly to an attached USB SSD device and it works. I have not yet tried it on any other Pi hardware, nor have I tried it with other types of USB flash media. Your Mileage May Vary.

Since conditional filters are specifically designed to accommodation different configurations, being able to "break out" specific configuration code into includable modules is a great way to organize code and avoid unreadable gunk.

In my specific case I'm doing both the things you suggested. I took the O/S specific config.txt files which include an "os_prefix" directive, naming them uniquely by O/S, and I also created a "master" config.txt file that has the selection logic in it. My master config.txt file tests certain gpio pins to see if they are pulled low, (selected), and then includes that particular O/S specific config file.

I have not figured out is how to test for nothing selected - i.e. pins 20, 26, 19, 16 all set to "1" (i.e. not selected) and then select a default condition. Since everything is evaluated, if I include a default "include" at the bottom of the selection stack, it will be the last thing selected and the last thing read.

Likewise, I have not figured out how to stop evaluation of the original config.txt file once a match has been made so that additional conditions are not evaluated.

Since I don't know the effect of including multiple copies of config.txt files within a single file, (which is equivalent to multiple instances of the same statements), I hesitate to just jumble things together and pray.

In any event, with the caveat that selecting more that one O/S at a time is unsupported and may produce unpredictable results, things are working as I expect.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: [SOLVED] Select an alternate config.txt file?

Fri Apr 23, 2021 12:18 am

Set the default option first. Later occurances of the same option in config.txt override earlier ones. If an option is set more than once, last value set wins.

Multiple conditionals are anded together, that's why you need the [all] directive at the end of the block.

Alternatively you could do something like this (don't cut'n'paste it's a non working example):

Code: Select all

# set up gpio

# default goes here

[gpio1=1]
[gpio2=0]
[gpio3=0]
[gpio4=0]
#set config 1 here
[all]

[gpio1=0]
[gpio2=1]
[gpio3=0]
[gpio4=0]
#set config 2 here
[all]
Any combination of GPIO states not explictly included will get the default config. A potential (2^n)-1 different configurations (where n = numberof GPIO in use.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Select an alternate config.txt file?

Fri Apr 23, 2021 12:33 am

jharris1993 wrote:
Thu Apr 22, 2021 5:49 pm
In my specific case I'm doing both the things you suggested. I took the O/S specific config.txt files which include an "os_prefix" directive, naming them uniquely by O/S
You might want to be carefull with that, I can see the potential for problems with things like device tree overlays, initrds, and kernels.

Where I've used os_prefix, I've always used a directory and copied the entire contents of the related OS's boot partition into that directory.

The default OS boot files are in the root of the boot partition, as is the master config.txt. That directory contains mutlitple OS specific directories, one per non default OS.

Couple of other things to consider:
  • If your multiple OS have different kernels you should probably disable automatic updates of the kernel in all of them otherwise you risk making one or more of the installed OS unbootable.
  • For non default OS, you might want to change how the boot partition gets mounted. Instead of mounting it on /boot, mount it somewhere else and link the correct os_prefix dir back to /boot. Or maybe use a bind mount instead of a link.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Re: [SOLVED] Select an alternate config.txt file?

Fri Apr 23, 2021 2:40 pm

thagrol wrote:
jharris1993 wrote:
Thu Apr 22, 2021 5:49 pm
In my specific case I'm doing both the things you suggested. I took the O/S specific config.txt files which include an "os_prefix" directive, naming them uniquely by O/S
You might want to be carefull with that, I can see the potential for problems with things like device tree overlays, initrds, and kernels.

Where I've used os_prefix, I've always used a directory and copied the entire contents of the related OS's boot partition into that directory.

The default OS boot files are in the root of the boot partition, as is the master config.txt. That directory contains mutlitple OS specific directories, one per non default OS.

In fact, that is precisely what I am doing. Hopefully. . . .

My boot partition is organized like this where "/" represents the root of the partition, NOT the root of the filesystem.

The "config.txt" in the root of the boot partition actually contains very little code. It configures the necessary gpio pins, maybe one or two other very generic things, (I don't remember right now), and then a series of conditional blocks that do nothing more than include the associated OSx_config.txt file. It is the associated OSx_config.txt file that does the heavy lifting for that specific operating system.

/OS_1/
/OS_2/
/OS_3/
/OS_4/
/config.txt
/OS1_config.txt
/OS2_config.txt
/OS3_config.txt
/OS4_config.txt
All the start.elf files from start.elf to start[n'].elf, about five or six files.
All the associated fixup files for each of the startup files from fixup.dat to fixup[n'].dat.
The idea is that, hopefully, this will boot on any USB compatible hardware, like a Pi-3. I am currently using this on a Pi-4.

Directories OS_1 to OS_4 contain the entire contents of the boot partition for that specific operating system, including the original config.txt associated with that operating system.

Directories OS_1 to OS_4 are attached to their respective OS's /boot directory via a bind mount in the fstab file, so that when the OS boots, the OS_x/ directory is overlayed on /boot. (the boot partition is mounted on a subdirectory in /mnt and that subdirectory is bind-mounted on /boot.)

One issue I can see right away is that the config.txt for a particular OS, in that OS's directory, can become out-of-sync with the one in the root of the boot partition. (i.e. /OSx_config.txt != /OS_x/config.txt after certain utilities are run, for example raspi-config.) What I am proposing, and what I will try, is a cron job that, on shutdown, copies the OS_x specific config.txt to OSx_config.txt I am wondering if the converse is useful - using a cron job on startup to copy OSx_config.txt back over OS_x/config.txt by copying it to /boot via the bind mount.

I have access to both structures via /mnt/boot/root/OS_x where /mnt/boot/root contains the root of the boot partition and ../OS_x is the OS specific subdirectory mounted over that operating system's /boot directory.
Set the default option first. Later occurrences of the same option in config.txt override earlier ones. If an option is set more than once, last value set wins.

Multiple conditionals are anded together, that's why you need the [all] directive at the end of the block.

Alternatively you could do something like this (don't cut'n'paste it's a non working example):

Code: Select all

# set up gpio

# default goes here

[gpio1=1]
[gpio2=0]
[gpio3=0]
[gpio4=0]
#set config 1 here
[all]

[gpio1=0]
[gpio2=1]
[gpio3=0]
[gpio4=0]
#set config 2 here
[all]

Any combination of GPIO states not explicitly included will get the default config. A potential (2^n)-1 different configurations (where n = number of GPIO in use.

Extremely interesting! This answers another question I was going to ask in a different thread. So, theoretically I could do something like this:
[gpio1=0]
[gpio2=0]
[gpio3=0]
[gpio4=0]
#set default here
[all]

[gpio1=1]
[gpio2=0]
[gpio3=0]
[gpio4=0]
#set config 1 here
[all]

--- etc for the rest of the potential GPIO conditions ---
[all]
Since subsequent configuration files inherit the setups in the original config.txt, and since only certain O/S's can use the 64 bit kernel, I place the 64 bit kernel flag in the individual configuration files.

If I am understanding, the structure
[conditional expression]
statements. . . .
[all]
. . . .is essentially the config.txt equivalent of an if/endif type block in other programming languages.

This is all extremely interesting. Not only am I NOT "taking you for granted", I am unbelievably grateful for your help!

Thanks!

Let me go try what I have learned.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: [SOLVED] Select an alternate config.txt file?

Fri Apr 23, 2021 3:06 pm

jharris1993 wrote:
Fri Apr 23, 2021 2:40 pm
Extremely interesting! This answers another question I was going to ask in a different thread. So, theoretically I could do something like this:
Yep. Buit don't forget that if none of your conditions are met the bootloader will still do its default thing.
If I am understanding, the structure
[conditional expression]
statements. . . .
[all]
. . . .is essentially the config.txt equivalent of an if/endif type block in other programming languages.
Essetntially, yes. But not as flexable. Be carefull of things like this:

Code: Select all

[condition one]
# stuff here runs when condition one is met
[condition two]
# stuff her only runs if condition one and condition two are both met
This is all extremely interesting. Not only am I NOT "taking you for granted", I am unbelievably grateful for your help!

Thanks!
You're welcome.

That sig is there becaue of a series of encounters with poster who were ungrateful, overly demanding, and aggresive that almost drove me off this forum. I figured a warning of the consequences of such bad behaviour might go a littel way to preventing it. Or at least preventing backlash if I do walk away from a thread.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Re: [SOLVED] Select an alternate config.txt file?

Fri Apr 23, 2021 8:00 pm

thagrol wrote:
Fri Apr 23, 2021 3:06 pm
This is all extremely interesting. Not only am I NOT "taking you for granted", I am unbelievably grateful for your help!

Thanks!
You're welcome.

That sig is there because of a series of encounters with poster who were ungrateful, overly demanding, and aggressive that almost drove me off this forum. I figured a warning of the consequences of such bad behavior might go a little way to preventing it. Or at least preventing backlash if I do walk away from a thread.

First:
Your time is your own. What you, (or anyone else, for that matter), does here is entirely voluntary and is a blessing for everyone else.

Anyone who thinks that they can "demand" things from ANYONE on these fora is not only a wannabe punter, but is a whole bunch of politically incorrect statements in US, UK, AU and Russian slang.

Second:
Anyone who gives you "backlash" for walking away from a thread, is a 'naffing idiot.
(I am translating "backlash" to mean busting onions, talking smack, and acting like an all-around kindergarten drop-out. Genuine concern about what happened to a long-time poster in a PM is not what I consider "backlash".)

Third:
It's a dark day when people have to remind others to play nice in the sandbox.

IMHO, I might disagree with what you say - or my own experimental findings may be different than yours - and that's OK. We're allowed to disagree and a bit of healthy disagreement and discussion is good for a forum. What I absolutely cannot stand is "being disagreeable".

You, (and others), are here to help and bless people with your knowledge of particular topics. I am a "regular" on another forum that specializes in robotics and I do my best to help. Maybe I don't know the answer, but I can either call-in someone who is a known subject matter expert, or suggest ways to discover the information on their own.

Likewise, (IMHO), "RTFM!!" should be on every forum's list of banned words as documentation may be so obtuse, (or poorly organized), that even diligent research may not uncover what is obvious to someone else.

I remember a time when I went nuts trying to find something. I searched high-and-low. I did everything but burn brown rice and sacrifice small rodents - all to no avail. Eventually - DAYS later - I discovered that using the word "parameter" caused my search to fail and I only found the answer I needed when I substituted "argument" for it. This is why "RTFM" burns me up so much.

In this case, you have been a wealth of knowledge and guidance. On a related topic, procount (of PINN fame) has been so incredibly patient with me and my screw-ball ideas that he really deserves to be sainted.

The Linux Mint forum is also a godsend.

Please stay around. Even if some idiot troll/karen/kevin decides to bust chops, please realize that they represent the slime at the shallow end of the gene-pool and that they will eventually reap what they sow.

Thank you so much for your help!

P. S.
I have had trouble duplicating your findings about conditionals being "anded" together.

Given the following:
(Assume the associated GPIO pins are configured as inputs with pull-ups and a "selected" pin becomes zero.)

Code: Select all

[gpio19=0]
Select this particular OS to boot

[gpio20=0]
Select the second OS

[gpio21=0]
Select the third OS

[gpio22=0]
Select the fourth OS

This works and selects the last OS selected if more than one bit is set. (I'm using dip-switches to select GPIO pins)

(note that there is no fall-through default. If something goes wrong, I get a system hung at the rainbow screen which is the desired outcome at this point.

However:

Code: Select all

[gpio19=0]
[gpio20=1]
[gpio21=1]
[gpio22=1]
Select the first OS to boot
[all]

[gpio19=1]
[gpio20=0]
[gpio21=1]
[gpio22=1]
Select the second OS
[all]

[gpio19=1]
[gpio20=1]
[gpio21=0]
[gpio22=1]
Select the third OS
[all]

[gpio19=1]
[gpio20=1]
[gpio21=1]
[gpio22=0]
Select the forth OS
[all]

. . . .seems to produce what can best be described as the logical "NAND" of all the bits - even if only one is selected at a time. For example, selecting GPIO 19 by pulling it to ground boots O/S 4, not 1. Likewise the second bit selects O/S 3 and other bit combinations are similarly screwy. If I go back and eliminate all the [pin]=1 statements, and all of the following [all] statements, it works again.

It seems to me that O/S 1 should ONLY be selected if the first bit is pulled low and the other three bits remain pulled high. Likewise for the other three operating system choices.

What am I not understanding rightly? It's probably something so insanely stupid that I will be smacking myself for a week!
picnic.gif
picnic.gif (9.2 KiB) Viewed 349 times
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: [SOLVED] Select an alternate config.txt file?

Sat Apr 24, 2021 3:45 pm

jharris1993 wrote:
Fri Apr 23, 2021 8:00 pm
I have had trouble duplicating your findings about conditionals being "anded" together.

Given the following:
(Assume the associated GPIO pins are configured as inputs with pull-ups and a "selected" pin becomes zero.)

Code: Select all

[gpio19=0]
Select this particular OS to boot

[gpio20=0]
Select the second OS

[gpio21=0]
Select the third OS

[gpio22=0]
Select the fourth OS

This works and selects the last OS selected if more than one bit is set. (I'm using dip-switches to select GPIO pins)

(note that there is no fall-through default. If something goes wrong, I get a system hung at the rainbow screen which is the desired outcome at this point.

However:

Code: Select all

[gpio19=0]
[gpio20=1]
[gpio21=1]
[gpio22=1]
Select the first OS to boot
[all]

[gpio19=1]
[gpio20=0]
[gpio21=1]
[gpio22=1]
Select the second OS
[all]

[gpio19=1]
[gpio20=1]
[gpio21=0]
[gpio22=1]
Select the third OS
[all]

[gpio19=1]
[gpio20=1]
[gpio21=1]
[gpio22=0]
Select the forth OS
[all]

. . . .seems to produce what can best be described as the logical "NAND" of all the bits - even if only one is selected at a time. For example, selecting GPIO 19 by pulling it to ground boots O/S 4, not 1. Likewise the second bit selects O/S 3 and other bit combinations are similarly screwy. If I go back and eliminate all the [pin]=1 statements, and all of the following [all] statements, it works again.

It seems to me that O/S 1 should ONLY be selected if the first bit is pulled low and the other three bits remain pulled high. Likewise for the other three operating system choices.

What am I not understanding rightly? It's probably something so insanely stupid that I will be smacking myself for a week! picnic.gif
My understanding is the same as yours. It's also what the documentation states (logical AND of conditions).

There is one thing though: don't let the GPIO float. Either use an external pull up (or down) or use the "gpio=" config.txt directive to set direction and pull before testing the state of the gpio:

Code: Select all

[all]
gpio=19=ip,pu
gpio=20=ip,pu
gpio=21=ip,pu
gpio=22=ip,pu

[gpio19=0]
[gpio20=1]
[gpio21=1]
[gpio22=1]
Select the first OS to boot
[all]

[gpio19=1]
[gpio20=0]
[gpio21=1]
[gpio22=1]
Select the second OS
[all]

[gpio19=1]
[gpio20=1]
[gpio21=0]
[gpio22=1]
Select the third OS
[all]

[gpio19=1]
[gpio20=1]
[gpio21=1]
[gpio22=0]
Select the forth OS
[all]
And you probably know this already but those are BCM pin numbers not physical or wiringpi numbers.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Re: [SOLVED] Select an alternate config.txt file?

Sun Apr 25, 2021 6:26 pm

I believe I have solved the mystery.

What appears to be happening is that the logical scope of a conditional is until either the beginning of the next conditional block or the end of the file.

Given the following:

GPIO's 16 - 19 are set up as inputs, pulled high.

GPIO 16, when pulled low, selects OS1
GPIO 17, when pulled low, selects OS2
GPIO 18, when pulled low, selects OS3
GPIO 19, when pulled low, selects OS4
Case 1:
# OS1 is selected via the dip-switches
GPIO 16 = 0
GPIO 17-19 = 1

Given the following conditional blocks:

# Should exclusively select OS1
[gpio16=0]
[gpio17=1]
[gpio18=1]
[gpio19=1]
os_prefix=OS1
[all]

# Should exclusively select OS2
[gpio16=1]
[gpio17=0]
[gpio18=1]
[gpio19=1]
os_prefix=OS2
[all]

# Should exclusively select OS3
[gpio16=1]
[gpio17=1]
[gpio18=0]
[gpio19=1]
os_prefix=OS3
[all]

# Should exclusively select OS4
[gpio16=1]
[gpio17=1]
[gpio18=1]
[gpio19=0]
os_prefix=OS4
[all]

Let's look at what happens when this runs:
(GPIO 16 = 0, GPIO 17-19 = 1)

First block:
# Should exclusively select OS1
[gpio16=0] = true
(there's nothing here, so nothing happens)
[gpio17=1] = true
(there's nothing here either, so nothing happens)
[gpio18=1] =true
(same as the first two, nothing happens)
[gpio19=1] = true
therefore "os_prefix" is set equal to "OS1"
[all] (closes the "gpio19" conditional)

Note that in every single case, in every single block, the first three GPIO pins are "don't care" since ONLY THE LAST STATEMENT does anything.

The result is that OS3 is selected because the last statement in the last block evaluates "false"
The additional result is that the only pin that selects anything is the last one because all the other selections are overridden by the last conditional block.

The only effective dip-switch is position 4, (GPIO 19), since it is always last, which has been confirmed by my own testing.

So, in essence, the conditional blocks work like this:
[condition 1]
If condition 1 is true, then do these things
. . . . . . . .
. . . . . . . .
[implied end of conditional block because a new condition starts]

[condition 2]
If condition 2 is true, then do these different things
. . . . . . . .
. . . . . . . .
[implied end of conditional block because a new condition starts]

[condition 3]
If condition 3 is true, then do this third set of things
. . . . . . . .
. . . . . . . .
[implied end of conditional block because a new condition starts]

[all] (which is functionally equivalent to "if (true)")
Do whatever is left over.

So, according to my analysis, there is absolutely no logical relationship between conditional blocks
.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: [SOLVED] Select an alternate config.txt file?

Sun Apr 25, 2021 7:49 pm

jharris1993 wrote:
Sun Apr 25, 2021 6:26 pm
So, according to my analysis, there is absolutely no logical relationship between conditional blocks
In light of your post, I've just reread the relevant documentation (https://www.raspberrypi.org/documentati ... itional.md)

I think it's more subtle than that.
Filters of the same type replace each other, so [pi2] overrides [pi1], because it is not possible for both to be true at once.

Filters of different types can be combined simply by listing them one after the other
So, while "gpio21=1" and "gpio22=0" are not mutualy exclusive the code seems to be treating them as such as both are "gpio" filters.

I guess I'll be logging at issue against the github for RPiOS and/or the bootloader firmware. Not going to expect a fix any time soon though, I doubt it's an issue that'll affect many people.

My appologies for feeding you duff information.

As for your aim, it looks like one GPIO per OS and last one wins. A single pole rotary swicth instead of a DIP switch might be the way forward. At least with a rotary switch it should eb safe to assume only one GPIO will be set at a time.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
thagrol
Posts: 4723
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: [SOLVED] Select an alternate config.txt file?

Sun Apr 25, 2021 9:46 pm

thagrol wrote:
Sun Apr 25, 2021 7:49 pm
I guess I'll be logging at issue against the github for RPiOS and/or the bootloader firmware. Not going to expect a fix any time soon though, I doubt it's an issue that'll affect many people.
https://github.com/raspberrypi/firmware/issues/1569
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

jharris1993
Posts: 244
Joined: Sat Oct 05, 2013 10:26 pm

Re: [SOLVED] Select an alternate config.txt file?

Mon Apr 26, 2021 6:56 pm

https://github.com/raspberrypi/firmware ... -827063255

Your honor, the prosecution rests. . . .

I just hope someone sees the original response, understands how silly and ill-considered it was, and re-opens the issue.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

Return to “Advanced users”