grub2: refactor code, add option to setup grub environment