/*static char *SCCSID = "@(#)mfs_term.c	6.1 90/11/18";*/
/*static char *SCCSID = "@(#)mfs_term.c 13.6 90/07/03";*/
#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include <fsd.h>
#include <fsh.h>

#include "heap.h"
#include "api.h"
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "pb_ifs.h"
#include "pbmacros.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include <cdib.h>
#include "codepage.h"

#include "pr_HEAP.h"

extern struct sffsd far *sffsdlist;
extern VOL_REOPEN_DATA far avrdList [];
extern USHORT cHVPB;
extern PB_MFSDATA fsddata;

/*
 ***	MFS_TERM - terminate the mini-FSD.
 *
 *	This function is called when stage 2 of the boot sequence is complete,
 *	and the mini-FSD is no longer necessary.  This routine frees all
 *	temporary memory that it allocated, and calls an initialization
 *	routine given by the FSD before terminating the mini-FSD.
 *
 *	Entry	No parameters
 *
 *	Returns error code (0 for success)
 */

int far pascal
MFS_TERM ()
{
    struct HEAPHDR far *p;
    struct HEAPHDR far *q;

#ifdef DEBUG
    dprintf("MFS_TERM: got here\n");
#endif

    avrdList[cHVPB].hVPB = 0;		/* terminate list for callback */

    (*(fsddata.CallBack))(sffsdlist, avrdList);

    /*
     * Free up all memory that the mini-FSD allocated.  This includes the cache,
     * the heap, the block map & segment used for code page upcasing.
     */

    p = fphdrHeap;
    do {
	q = p->fphdrFwd;
	FSH_SEGFREE (SELECTOROF (p));
	p = q;
    } while (p != fphdrHeap);

    FSH_SEGFREE (SELECTOROF (abufCache [0].pblkData));
    FSH_SEGFREE (SELECTOROF (pusBlockMap));
    FSH_SEGFREE (SELECTOROF (pbCaseCmpBuf));

#ifdef DEBUG
    dprintf("MFS_TERM: leaving\n");
#endif

    return (NO_ERROR);
}
