Add a disable_msd to config to totally disable msd
authorJim Morris <morris@wolfman.com>
Wed, 30 Jul 2014 01:28:45 +0000 (18:28 -0700)
committerJim Morris <morris@wolfman.com>
Wed, 30 Jul 2014 01:30:11 +0000 (18:30 -0700)
src/main.cpp

index f34efd2..8687c0d 100644 (file)
@@ -65,8 +65,7 @@ SDCard sd  __attribute__ ((section ("AHBSRAM0"))) (P0_9, P0_8, P0_7, P0_6);
 
 USB u __attribute__ ((section ("AHBSRAM0")));
 USBSerial usbserial __attribute__ ((section ("AHBSRAM0"))) (&u);
-USBMSD msc __attribute__ ((section ("AHBSRAM0"))) (&u, &sd);
-//USBMSD *msc= NULL;
+USBMSD *msc= NULL;
 DFU dfu __attribute__ ((section ("AHBSRAM0"))) (&u);
 
 SDFAT mounter __attribute__ ((section ("AHBSRAM0"))) ("sd", &sd);
@@ -97,12 +96,16 @@ void init() {
     kernel->use_leds= !kernel->config->value( disable_leds_checksum )->by_default(false)->as_bool();
 
     // attempt to be able to disable msd in config
-    // if(!kernel->config->value( disable_msd_checksum )->by_default(false)->as_bool()){
-    //     msc= new USBMSD(&u, &sd);
-    // }else{
-    //     msc= NULL;
-    //     kernel->streams->printf("MSD is disabled\r\n");
-    // }
+    if(!kernel->config->value( disable_msd_checksum )->by_default(false)->as_bool()){
+        // HACK to zero the memory USBMSD uses as it and its objects seem to not initialize properly in the ctor
+        size_t n= sizeof(USBMSD);
+        void *v = AHB0.alloc(n);
+        memset(v, 0, n); // clear the allocated memory
+        msc= new(v) USBMSD(&u, &sd); // allocate object using zeroed memory
+    }else{
+        msc= NULL;
+        kernel->streams->printf("MSD is disabled\r\n");
+    }
 
     bool sdok= (sd.disk_initialize() == 0);
 
@@ -158,16 +161,11 @@ void init() {
 
     // Create and initialize USB stuff
     u.init();
-    //if(sdok) { // only do this if there is an sd disk
-    //    msc= new USBMSD(&u, &sd);
-    //    kernel->add_module( msc );
-    //}
 
-    // if(msc != NULL){
-    //     kernel->add_module( msc );
-    // }
+    if(sdok && msc != NULL){
+        kernel->add_module( msc );
+    }
 
-    kernel->add_module( &msc );
     kernel->add_module( &usbserial );
     if( kernel->config->value( second_usb_serial_enable_checksum )->by_default(false)->as_bool() ){
         kernel->add_module( new USBSerial(&u) );