diff --git a/ATMegaCard/Makefile b/ATMegaCard/Makefile
index 4288b3519326f3857feba8828ef383dea1203916..8e5e590a5d07800e71d2f69018e7b393ef05473f 100644
--- a/ATMegaCard/Makefile
+++ b/ATMegaCard/Makefile
@@ -146,8 +146,11 @@ AVRDUDE_PORT = usb        # programmer connected to USB port
 #AVRDUDE_PORT = lpt1	       # programmer connected to parallel port
 
 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex $(FUSE_BITS)
-#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
 
+ifneq ($(KEY),)
+	EEPROM_GEN_CMD=./eeprom_gen.rb $(TARGET).eep $(KEY)
+	AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
+endif
 #AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -b 115200 -c $(AVRDUDE_PROGRAMMER)
 AVRDUDE_FLAGS = -B 1 -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
 
@@ -227,7 +230,7 @@ ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
 
 
 # Default target.
-all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex $(TARGET).eep \
+all: begin gccversion sizebefore $(TARGET).elf $(TARGET).hex \
 	$(TARGET).lss $(TARGET).sym sizeafter finished end
 
 
@@ -286,7 +289,7 @@ extcoff: $(TARGET).elf
 
 
 # Program the device.  
-program: $(TARGET).hex $(TARGET).eep
+program: $(TARGET).hex eepromgen
 	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
 
 
@@ -298,11 +301,11 @@ program: $(TARGET).hex $(TARGET).eep
 	@echo $(MSG_FLASH) $@
 	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
 
-%.eep: %.elf
-	@echo
-	@echo $(MSG_EEPROM) $@
-	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
-	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
+# %.eep: %.elf
+# 	@echo
+# 	@echo $(MSG_EEPROM) $@
+# 	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
+# 	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
 
 # Create extended listing file from ELF output file.
 %.lss: %.elf
@@ -317,6 +320,9 @@ program: $(TARGET).hex $(TARGET).eep
 	avr-nm -n $< > $@
 
 
+eepromgen:
+	@echo
+	$(EEPROM_GEN_CMD)
 
 # Link: create ELF output file from object files.
 .SECONDARY : $(TARGET).elf
@@ -394,5 +400,5 @@ clean_list :
 
 # Listing of phony targets.
 .PHONY : all begin finish end sizebefore sizeafter gccversion coff extcoff \
-	clean clean_list program
+	clean clean_list program eepromgen