Detecting boot source with s2 button press: beaglebone black?

I am working on a yocto project with BBB and have integrated mender ota update.
I have built two images one for sd card and the other for emmc as the first one is mmc0 and latter is mmc1. I had to do this because, mender requires to explicitly update it’s variable. But this definitely is not a flexible approach. Now I want to create a single image for both sdcard and emmc. For that, I would first need to detect the boot source when the s2 button on BBB is pressed. On reading the source, I can write it to an environment variable and then update the mender variable. So, I am now stuck in reading the boot source, I assume that some register value is updated or maybe some method is called after the s2 button is pressed. If anyone has idea on achieving this?