I would keep the Pi and Mega cards in separated enough so that I could unplug the pi and with USB extension cable upload new firmware from laptop.
But if you really want to study this and you have nothing to start, here is my 5 minutes work.
I haven't even tried to use arduino on pi, but on my desktop it feels avr-gcc really likes to use CPU. That said, I would keep the compilation process on real computer with infinite memory and unlimited CPU power.
Arduino must build the hex file somewhere. Search it and copy it to raspberry pi and use avrdude to flash it.
[
www.arduino.cc] gives some ideas what to look. Maybe arduino-ide can output more detailed debug output to show what it really does.
My experience with arduino is really limited.
My educated guess is something like below but it needs something more to work.
avrdude -p m2560 -c wiring -P /dev/ttyUSB0
-p m2560 is mega 2560
-c wiring is protocol/burner/whatever to use to upload the binary. Wiring was the first version of high abstraction programming environment which later became known as Arduino.
-P port and /dev/ttyUSB0 is first USB serial port.
Have a look Octoprint plugins. There are some eeprom related plugins, if those use avrdude to program the eeprom, then writing Reflash your arduino plugin would be quite easy if you are familiar with Python.