I would suggest you take a close look at how Chilipeppr implements the FW download function.
It actually runs in the SPJS environment.
The FW download process firsts sts tinyG into bootloader mode using $boot=1, then disconnects the USB port and calls a configured version of avrdude.
Chilipepper has avrdude binaries for Winx, Linux and Mac.
Your challenge might be finding an avrdude compiled to run on your Mega2560
tinyG-Updater, found here
does essentially the sam