@@ -27,6 +27,8 @@ typedef struct
2727 dmdrvi_context_t driver_context ; // Driver-specific context
2828 Dmod_Context_t * driver ; // Driver module context
2929 dmdrvi_dev_num_t dev_num ; // Device number assigned to the driver
30+ bool was_loaded ; // Indicates if the driver was loaded by dmdevfs
31+ bool was_enabled ; // Indicates if the driver was enabled by dmdevfs
3032} driver_node_t ;
3133
3234/**
@@ -45,6 +47,7 @@ struct dmfsi_context
4547// ============================================================================
4648static int configure_drivers (dmfsi_context_t ctx , const char * driver_name , const char * config_path );
4749static driver_node_t * configure_driver (const char * driver_name , dmini_context_t config_ctx );
50+ static int unconfigure_drivers (dmfsi_context_t ctx );
4851static bool is_file (const char * path );
4952static void read_base_name (const char * path , char * base_name , size_t name_size );
5053static dmini_context_t read_driver_for_config (const char * config_path , char * driver_name , size_t name_size , const char * default_driver );
@@ -117,8 +120,9 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, dmfsi_context_t, _init, (const cha
117120 if (res != DMFSI_OK )
118121 {
119122 DMOD_LOG_ERROR ("dmdevfs: Failed to configure drivers\n" );
123+ unconfigure_drivers (ctx );
120124 dmlist_destroy (ctx -> drivers );
121- Dmod_Free (( void * ) ctx -> config_path );
125+ Dmod_Free (ctx -> config_path );
122126 Dmod_Free (ctx );
123127 return NULL ;
124128 }
@@ -127,24 +131,29 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, dmfsi_context_t, _init, (const cha
127131}
128132
129133/**
130- * @brief Deinitialize the file system
134+ * @brief Validate the file system context
131135 */
132- dmod_dmfsi_dif_api_declaration ( 1.0 , dmdevfs , int , _deinit , (dmfsi_context_t ctx ) )
136+ dmod_dmfsi_dif_api_declaration ( 1.0 , dmdevfs , int , _context_is_valid , (dmfsi_context_t ctx ) )
133137{
134- if (ctx )
135- {
136- // TODO: Cleanup driver context
137- Dmod_Free (ctx );
138- }
139- return DMFSI_OK ;
138+ return (ctx && ctx -> magic == DMDEVFS_CONTEXT_MAGIC ) ? 1 : 0 ;
140139}
141140
142141/**
143- * @brief Validate the file system context
142+ * @brief Deinitialize the file system
144143 */
145- dmod_dmfsi_dif_api_declaration ( 1.0 , dmdevfs , int , _context_is_valid , (dmfsi_context_t ctx ) )
144+ dmod_dmfsi_dif_api_declaration ( 1.0 , dmdevfs , int , _deinit , (dmfsi_context_t ctx ) )
146145{
147- return (ctx && ctx -> magic == DMDEVFS_CONTEXT_MAGIC ) ? 1 : 0 ;
146+ if (!dmfsi_dmdevfs_context_is_valid (ctx ))
147+ {
148+ DMOD_LOG_ERROR ("dmdevfs: Invalid context in deinit\n" );
149+ return DMFSI_ERR_INVALID ;
150+ }
151+
152+ unconfigure_drivers (ctx );
153+ dmlist_destroy (ctx -> drivers );
154+ Dmod_Free (ctx -> config_path );
155+ Dmod_Free (ctx );
156+ return DMFSI_OK ;
148157}
149158
150159/**
@@ -380,15 +389,8 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _closedir, (dmfsi_context_t c
380389 * @brief Create a directory
381390 */
382391dmod_dmfsi_dif_api_declaration ( 1.0 , dmdevfs , int , _mkdir , (dmfsi_context_t ctx , const char * path ) )
383- {
384- if (dmfsi_dmdevfs_context_is_valid (ctx ) == 0 )
385- {
386- DMOD_LOG_ERROR ("dmdevfs: Invalid context in mkdir\n" );
387- return DMFSI_ERR_INVALID ;
388- }
389-
390- // TODO: Implement directory creation
391- return DMFSI_ERR_GENERAL ;
392+ {
393+ return DMFSI_ERR_INVALID ; // Not supported
392394}
393395
394396/**
@@ -540,6 +542,8 @@ static driver_node_t* configure_driver(const char* driver_name, dmini_context_t
540542 return NULL ;
541543 }
542544
545+ driver_node -> was_loaded = was_loaded ;
546+ driver_node -> was_enabled = was_enabled ;
543547 driver_node -> driver = driver ;
544548 driver_node -> driver_context = dmdrvi_create (config_ctx , & driver_node -> dev_num );
545549 if (driver_node -> driver_context == NULL )
@@ -553,6 +557,37 @@ static driver_node_t* configure_driver(const char* driver_name, dmini_context_t
553557 return driver_node ;
554558}
555559
560+ /**
561+ * @brief Unconfigure and unload all drivers
562+ */
563+ static int unconfigure_drivers (dmfsi_context_t ctx )
564+ {
565+ if (ctx == NULL || ctx -> drivers == NULL )
566+ {
567+ return DMFSI_ERR_INVALID ;
568+ }
569+
570+ size_t list_size = dmlist_size (ctx -> drivers );
571+ for (size_t i = 0 ; i < list_size ; i ++ )
572+ {
573+ driver_node_t * driver_node = (driver_node_t * )dmlist_get (ctx -> drivers , i );
574+ if (driver_node != NULL )
575+ {
576+ dmod_dmdrvi_free_t dmdrvi_free = Dmod_GetDifFunction (driver_node -> driver , dmod_dmdrvi_free_sig );
577+ if (dmdrvi_free != NULL )
578+ {
579+ dmdrvi_free (driver_node -> driver_context );
580+ }
581+ cleanup_driver_module (Dmod_GetName (driver_node -> driver ), driver_node -> was_loaded , driver_node -> was_enabled );
582+ Dmod_Free (driver_node );
583+ }
584+ }
585+
586+ dmlist_clear (ctx -> drivers );
587+
588+ return DMFSI_OK ;
589+ }
590+
556591/**
557592 * @brief Check if a path is a file
558593 */
0 commit comments