#===============================================================================
#
# EFS Libs
#
# GENERAL DESCRIPTION
#    build script
#
# Copyright (c) 2009-2011 by QUALCOMM, Incorporated.
# All Rights Reserved.
# QUALCOMM Proprietary/GTDR
#
#-------------------------------------------------------------------------------
#
#  $Header$  $DateTime$  $Author$
#  $Change$
#                      EDIT HISTORY FOR FILE
#
#  This section contains comments describing changes made to the module.
#  Notice that changes are listed in reverse chronological order.
#
# when        who  what, where, why
# ----------  ---  -------------------------------------------------------------
# 2011-11-11  rp   Removed dependency on FEATURE_RCINIT.
# 2011-11-03  dks  Make Hotplug independent of EFS.
# 2011-11-03  rp   Compile EFS for GSS product image.
# 2011-10-13  dks  Add efs_utils_lib once to boot images.
# 2011-10-11  nr   Add zlib support for zip and zipped tar.
# 2011-10-05  wek  Create a list of error codes in a text file.
# 2011-10-03  dks  Move efs_get_errno_address to fs_errno.c.
# 2011-08-17  rp   Add EFS OTP (One-Time-Programmable) support.
# 2011-08-14  wek  Get the errno from global header file.
# 2011-06-02  dks  Migrate from TMC to rcinit.
# 2011-03-25  dks  Support Golden-Copy in RAM for fusion.
# 2011-03-03  dks  Add device setup calls.
# 2010-12-03  rp   Featurize FS-TAR module.
# 2010-11-15  rp   Replace Boot cookies with EFS cookies.
# 2010-11-12  dks  Added fs_boot_cookie.c
# 2010-11-08  wek  Remove impractical and unused paranoia module.
# 2010-10-20  dks  Added fs_cookie.c
# 2010-10-14  vr   Remove default inclusion of HOTPLUG for boot.
# 2010-08-26  rp   Enable HFAT for BOOT module based on flag.
# 2010-07-19  wek  Add file for memory allocation.
# 2010-06-28  wek  Supress EFS1 related warnings.
# 2010-06-14  vr   Hotplug to compile without EFS for 8660 SBL1.
# 2010-05-07  rp   Add Ram File System.
# 2010-03-23  dks  Add libs to QDSP6 to compile for Voyager.
# 2010-03-05  vr   Add support for QEFS2 in BOOT
# 2010-02-26  yog  Map legacy messages to MSG_SSID_FS
# 2010-02-10  rp   Added fs_osal.c.
# 2010-02-02  wek  Added SECURMSM to internal CoreBSP dependencies.
# 2010-02-01  wek  Chaged source path to use core/storage.
#
#===============================================================================

Import('env')
env = env.Clone()
env.LoadToolScript("efs_errno_builder.py")
env.Append(CPPDEFINES=['MSG_BT_SSID_DFLT=MSG_SSID_FS'])

#-------------------------------------------------------------------------------
# Source PATH
#-------------------------------------------------------------------------------
SRCPATH = "${BUILD_ROOT}/core/storage/efs/src"

env.VariantDir('${BUILDPATH}', SRCPATH, duplicate=0)

#-------------------------------------------------------------------------------
# External depends within CoreBSP
#-------------------------------------------------------------------------------
env.RequireExternalApi([
   'DSM',
   'MODEM_1X',
   'MODEM_GERAN',
   'MODEM_PMIC',
   'MODEM_RF',
   'MODEM_SERVICES',
   'MODEM_WCDMA',
])

#-------------------------------------------------------------------------------
# Internal depends within CoreBSP
#-------------------------------------------------------------------------------
CBSP_API = [
   'BOOT',
   'DAL',
   'DEBUGTOOLS',
   'HAL',
   'HWENGINES',
   'MPROC',
   'SECUREMSM',
   'SERVICES',
   'STORAGE',
   'SYSTEMDRIVERS',
   'WIREDCONNECTIVITY',

   # needs to be last as it may contain wrong comdef.h
   'KERNEL',
]

env.RequirePublicApi(CBSP_API)
env.RequireRestrictedApi(CBSP_API)

#-------------------------------------------------------------------------------
# Sources, libraries
#-------------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# AMSS source files
#-----------------------------------------------------------------------------

EFS_SOURCES = [
    '${BUILDPATH}/fs_benchmark.c',
    '${BUILDPATH}/fs_boot_cookie.c',
    '${BUILDPATH}/fs_buffer.c',
    '${BUILDPATH}/fs_compat.c',
    '${BUILDPATH}/fs_convert_utf.c',
    '${BUILDPATH}/fs_cookie.c',
    '${BUILDPATH}/fs_db.c',
    '${BUILDPATH}/fs_desc.c',
    '${BUILDPATH}/fs_device.c',
    '${BUILDPATH}/fs_device_efs_flash.c',
    '${BUILDPATH}/fs_device_efs_mnand.c',
    '${BUILDPATH}/fs_device_efs_ramfs.c',
    '${BUILDPATH}/fs_device_efs_rmts.c',
    '${BUILDPATH}/fs_device_ftl_flash.c',
    '${BUILDPATH}/fs_device_otp_flash.c',
    '${BUILDPATH}/fs_diag.c',
    '${BUILDPATH}/fs_diag_access.c',
    '${BUILDPATH}/fs_efs2.c',
    '${BUILDPATH}/fs_efs2_names.c',
    '${BUILDPATH}/fs_estdio.c',
    '${BUILDPATH}/fs_ext_hfat.c',
    '${BUILDPATH}/fs_extfs.c',
    '${BUILDPATH}/fs_factory.c',
    '${BUILDPATH}/fs_fdi.c',
    '${BUILDPATH}/fs_fdi_mnt.c',
    '${BUILDPATH}/fs_fdi_vnode_devops.c',
    '${BUILDPATH}/fs_fdi_vnode_dops.c',
    '${BUILDPATH}/fs_fdi_vnode_fops.c',
    '${BUILDPATH}/fs_fdi_vnode_itemops.c',
    '${BUILDPATH}/fs_fdi_vnode_sops.c',
    '${BUILDPATH}/fs_flash_dal_shim.c',
    '${BUILDPATH}/fs_ftl.c',
    '${BUILDPATH}/fs_ftl_cache.c',
    '${BUILDPATH}/fs_global_lock.c',
    '${BUILDPATH}/fs_inode.c',
    '${BUILDPATH}/fs_journal.c',
    '${BUILDPATH}/fs_lib.c',
    '${BUILDPATH}/fs_logr.c',
    '${BUILDPATH}/fs_malloc.c',
    '${BUILDPATH}/fs_mnand_bird_view.c',
    '${BUILDPATH}/fs_mnand_log.c',
    '${BUILDPATH}/fs_mnand_pcache.c',
    '${BUILDPATH}/fs_mnand_pm.c',
    '${BUILDPATH}/fs_mnand_ptbl.c',
    '${BUILDPATH}/fs_mnand_region.c',
    '${BUILDPATH}/fs_mnand_super.c',
    '${BUILDPATH}/fs_mount.c',
    '${BUILDPATH}/fs_namei.c',
    '${BUILDPATH}/fs_nodev.c',
    '${BUILDPATH}/fs_otp.c',
    '${BUILDPATH}/fs_otp_pcache.c',
    '${BUILDPATH}/fs_pathbuff.c',
    '${BUILDPATH}/fs_pm.c',
    '${BUILDPATH}/fs_pm_gc.c',
    '${BUILDPATH}/fs_pm_log.c',
    '${BUILDPATH}/fs_pm_ptable_nand.c',
    '${BUILDPATH}/fs_pm_ptable_nor.c',
    '${BUILDPATH}/fs_pm_super.c',
    '${BUILDPATH}/fs_privatedir.c',
    '${BUILDPATH}/fs_public.c',
    '${BUILDPATH}/fs_ramfs_pm.c',
    '${BUILDPATH}/fs_realpath.c',
    '${BUILDPATH}/fs_rmts_pm.c',
    '${BUILDPATH}/fs_rmts_super.c',
    '${BUILDPATH}/fs_rollback.c',
    '${BUILDPATH}/fs_romfs.c',
    '${BUILDPATH}/fs_stdio.c',
    '${BUILDPATH}/fs_sync.c',
    '${BUILDPATH}/fs_task.c',
    '${BUILDPATH}/fs_unicode.c',
    '${BUILDPATH}/fs_unicode_sjis.c',
    '${BUILDPATH}/fs_unicode_utf8.c',
    '${BUILDPATH}/fs_upgrade.c',
    '${BUILDPATH}/fs_vnode.c',
    '${BUILDPATH}/fs_zip.c',
    '${BUILDPATH}/fs_zip_malloc.c'
]
efs_obj = env.Object(EFS_SOURCES)
efs_lib = env.Library('${BUILDPATH}/efs', efs_obj)

#-----------------------------------------------------------------------------
# ROMFS source files
#-----------------------------------------------------------------------------

EFS_ROMFS_SOURCES = [
   '${BUILDPATH}/fs_benchmark.c',
   '${BUILDPATH}/fs_cookie.c',
   '${BUILDPATH}/fs_cookie_handler.c',
   '${BUILDPATH}/fs_convert_utf.c',
   '${BUILDPATH}/fs_desc.c',
   '${BUILDPATH}/fs_ext_hfat.c',
   '${BUILDPATH}/fs_extfs.c',
   '${BUILDPATH}/fs_global_lock.c',
   '${BUILDPATH}/fs_inode.c',
   '${BUILDPATH}/fs_mount.c',
   '${BUILDPATH}/fs_namei.c',
   '${BUILDPATH}/fs_nodev.c',
   '${BUILDPATH}/fs_public.c',
   '${BUILDPATH}/fs_rmts_cookie.c',
   '${BUILDPATH}/fs_romfs.c',
   '${BUILDPATH}/fs_unicode.c',
   '${BUILDPATH}/fs_unicode_utf8.c',
   '${BUILDPATH}/fs_unicode_sjis.c',
   '${BUILDPATH}/fs_vnode.c'
]

efs_romfs_obj = env.Object(EFS_ROMFS_SOURCES)
efs_romfs_lib = env.Library('${BUILDPATH}/efs_romfs', efs_romfs_obj)

#-----------------------------------------------------------------------------
# EFS UTILS source files
#-----------------------------------------------------------------------------

EFS_UTILS_SOURCES = [
   '${BUILDPATH}/fs_counters.c',
   '${BUILDPATH}/fs_errno.c',
   '${BUILDPATH}/fs_osal.c',
   '${BUILDPATH}/fs_util.c'
]

efs_utils_obj = env.Object(EFS_UTILS_SOURCES)
efs_utils_lib = env.Library('${BUILDPATH}/efs_utils', efs_utils_obj)

#----------------------------------------------------------------------------
# Pre-process EFS errno list.
#----------------------------------------------------------------------------

efs_errno_list = env.FsErrnoListBuilder ('${BUILDPATH}/fs_errno_list.txt',
                                         '${BUILDPATH}/fs_errno_list.c')
env.Depends ([efs_lib, efs_romfs_lib, efs_utils_lib], efs_errno_list);

#-------------------------------------------------------------------------------
# EFS config
#-------------------------------------------------------------------------------

if 'BUILD_BOOT_CHAIN' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_BOOT_ENV"])
  env.Append(CPPDEFINES = ["FEATURE_EFS_COMPILE_WITHOUT_MD5"])
  env.Append(CPPDEFINES = ["FEATURE_EFS_COMPILE_WITHOUT_REX"])
  env.Append(CPPDEFINES = ["FEATURE_EFS_COMPILE_WITHOUT_DIAG"])
  env.Append(CPPDEFINES = ["FEATURE_FS_HOTPLUG_DISABLE_TASK"])
  env.Append(CPPDEFINES = ["HOTPLUG_HAVE_PARTITION_SUPPORT"])
  if 'USES_QEFS2_IN_BOOT' in env:
    env.PrintDebugInfo ('efs', "EFS QEFS2 in BOOT variant\n")
  elif 'USES_HOTPLUG_WITHOUT_EFS' in env:
    env.PrintDebugInfo ('efs', "HOTPLUG without EFS variant\n")
    env.Append (CPPDEFINES = ["FEATURE_HOTPLUG_COMPILE_WITHOUT_EFS"])
  else:
    env.PrintDebugInfo ('efs', "EFS ROMFS in BOOT variant\n")
    env.Append (CPPDEFINES = ["FEATURE_EFS_ROMFS"])
else:
  env.PrintDebugInfo ('efs', "EFS in AMSS variant")


if 'USES_FS_HOTPLUG_SD' in env:
  env.Append(CPPDEFINES = ["FEATURE_FS_HOTPLUG_SD"])

if 'USES_FS_FTL' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_FTL"])
  env.Append(CPPDEFINES = ["FEATURE_FS_HOTPLUG_FTL"])

if 'USES_FS_HOTPLUG_SD_CPRM' in env:
  env.Append(CPPDEFINES = ["FEATURE_FS_HOTPLUG_SD_CPRM"])

if 'USES_FS_HOTPLUG_USB' in env:
  env.Append(CPPDEFINES = ["FEATURE_FS_HOTPLUG_USB"])

if ('USES_FS_HOTPLUG_SD' in env)        or \
   ('USES_FS_FTL' in env)       or \
   ('USES_FS_HOTPLUG_SD_CPRM' in env)   or \
   ('USES_FS_HOTPLUG_USB' in env):
  env.Append(CPPDEFINES = ["FEATURE_FS_HOTPLUG"])
  if ('USES_FS_NO_EFS2' in env) :
    env.Append(CPPDEFINES = ["FEATURE_HOTPLUG_COMPILE_WITHOUT_EFS"])


if 'USES_FS_HFAT' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_EXTFS"])
  env.Append(CPPDEFINES = ["FEATURE_EFS_EXTFS_HFAT"])

if 'USES_FS_TAR' in env:
  env.Append(CPPDEFINES = ["FEATURE_FS_TAR"])

if 'USES_EFS_SETUP_DEVICE' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_SETUP_DEVICE"])

if 'USES_BACKUP_FUSION_PARTITION' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_BACKUP_FUSION_PARTITION"])

if 'USES_NEED_DUAL_SYNC' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_NEED_DUAL_SYNC"])

if 'USES_EFS_OTP' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_OTP"])

if 'USES_FS_ZIP_TAR' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_ZIP"])
  env.Append(CPPDEFINES = ["FEATURE_FS_TAR"])
  env.Append(CPPDEFINES = ["FEATURE_FS_ZIP_TAR"])

if 'USES_RCINIT' in env:
  env.Append(CPPDEFINES = ["FEATURE_EFS_USE_RCINIT"])

#-------------------------------------------------------------------------------
# Supress all EFS1 warnings on EFS modules.
#-------------------------------------------------------------------------------
env.Append(CPPDEFINES = ["FEATURE_EFS_SUPPRESS_EFS1_WARNINGS"])

#-------------------------------------------------------------------------------
# Add Libraries to image
#-------------------------------------------------------------------------------

env.AddLibsToImage(
  ['MODEM_IMAGE', 'CBSP_MODEM_IMAGE', 'APPS_IMAGE', 'CBSP_APPS_IMAGE',
   'SINGLE_IMAGE', 'CBSP_SINGLE_IMAGE', 'QDSP6_SW_IMAGE', 'CBSP_QDSP6_SW_IMAGE',
   'OSBL_BOOT_IMAGE', 'SBL1_BOOT_IMAGE', 'SBL2_BOOT_IMAGE', 'SBL3_BOOT_IMAGE',
   'CORE_GSS'],
   efs_utils_lib)

if not 'USES_FS_NO_EFS2' in env:
  env.AddLibsToImage(
  ['MODEM_IMAGE', 'CBSP_MODEM_IMAGE', 'APPS_IMAGE', 'CBSP_APPS_IMAGE',
   'SINGLE_IMAGE', 'CBSP_SINGLE_IMAGE', 'QDSP6_SW_IMAGE', 'CBSP_QDSP6_SW_IMAGE',
   'CORE_GSS'],
   efs_lib)

if 'USES_QEFS2_IN_BOOT' in env:
  env.AddLibsToImage(['OSBL_BOOT_IMAGE'], efs_lib)
elif not 'USES_HOTPLUG_WITHOUT_EFS' in env:
  env.AddLibsToImage(['DBL_BOOT_IMAGE','OSBL_BOOT_IMAGE','SBL3_BOOT_IMAGE'],
                     efs_romfs_lib)

#-------------------------------------------------------------------------------

if 'USES_RCINIT' in env:
  RCINIT_IMG = ['CORE_MODEM', 'CORE_QDSP6_SW', 'CORE_GSS']
  env.AddRCInitTask(           # FS
   RCINIT_IMG,                 # define TMC_RCINIT_REXTASK_FS
   {
     'sequence_group'             : 'RCINIT_GROUP_0',                # required
     'thread_name'                : 'fs',                            # required
     'thread_entry'               : 'fs_task',
     'stack_size_bytes'           : '4096',
     'priority_amss_order'        : 'FS_PRI_ORDER',
     'cpu_affinity'               : 'REX_ANY_SMT_MASK',
   })
